I am a very beginner about JSON…
How can I get the value of “3037904” from the following JSON Object in Java program?
{“query”:{“pages”:{“3037904”:{“pageid”:3037904,”ns”:0,”title”:”Kempinski”,
“categories”:[{“ns”:14,”title”:”Category:Companies established in 1897″},{“ns”:14,”title”:”Category:Hotel chains”},{“ns”:14,”title”:”Category:Kempinski Hotels”}]}}}}
I tried to
JSONObject query = json.getJSONObject("query");
int pages = query.getInt("pages");
But it takes
"{"3037904":{"pageid":3037904,"ns":0,"title":"Kempinski",
"categories":[{"ns":14,"title":"Category:Companies established in 1897"},{"ns":14,"title":"Category:Hotel chains"},{"ns":14,"title":"Category:Kempinski Hotels"}]}}}}",
not only “3037904”.
You’ll need to do a little more work with your
JSONObject.In this answer I’ll assume that you want to get at that
pageidvalue.Let’s only assume the existence of
pageidin the nesting – at a specific level:Your JSON input seems peculiar in that the first nested object has a
pagesfield, that contains another object. The name in plural,pages, and the nested object – which duplicates the key containing the object as apageidkey within the object – suggests thatpagesshould be an array of multiple such objects.