Pompili Manuel
Pompili Manuel

Reputation: 3

How to access nested objects of a Json File

the string I have into "jsonString" is the content of this link: http://85.18.173.82/cineca/wp5/json/events.json

Now I want the value "Day" of the second "Event".

JSONObject o = new JSONObject(jsonString);
String day = o.getString("XXXXXXXXXX");
System.out.println(day);

What does I have to put as argument of o.getString?

Many thanks

Upvotes: 0

Views: 339

Answers (2)

Dinesh Kumar DJ
Dinesh Kumar DJ

Reputation: 488

JSONObject obj = new JSONObject(json);

JSONArray array = obj.getJSONArray("Events");
for(int i = 0 ; i < array.length() ; i++){
    System.out.println(array.getJSONObject(i).getJSONObject("Event").getString("Day"));
}

In this way, you can access, thanks.

Upvotes: 2

pnt
pnt

Reputation: 1916

The way you're constructing your JSONObject is wrong. By using this constructor you're not reading the json from that URL, you're actually using that string as a json representation (which it is not).

If you want to first read the json from your URL you'll have to do an HTTP GET request and then construct a JSONObject out of the response.

For more info, take a look at JSONObject docs

Upvotes: 1

Related Questions