I am trying read JSON data from this link.
I am able to read the entire data inside pages attribute using :
JSONObject data=(JSONObject)new JSONTokener(IOUtils.toString(new URL("https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&explaintext=&titles=Russell%27s_paradox"))).nextValue();
String pageName=data.getJSONObject("query").getString("pages");
System.out.println(pageName);
I want to get the data from "extract" attribute next from the above mentioned link, which I am unable to. I am new with this and I can't find any resources to learn this.
I tried the following code but I am getting a JSONException.
JSONArray arr=data.getJSONArray("pages");
for(int i=0;i<arr.length();i++){
String def=arr.getJSONObject(i).getString("extract");
System.out.println(def);
}
Help.
The structure of the JSON you're trying to parse is this:
That is,
pages
insidequery
is an object, not an array. And then,extract
is within another nested object, with key46095
. You can get to theextract
field like this: