Benben
Benben

Reputation: 1455

How can I fetch the value from JSON in Java program?

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".

Upvotes: 1

Views: 2092

Answers (2)

pb2q
pb2q

Reputation: 59637

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 pageid value.

Let's only assume the existence of pageid in the nesting - at a specific level:

// "query" is the top-level object: 
JSONObject query = json.getJSONObject("query");
// "pages" is a field of "query"
JSONObject pages = query.getJSONObject("pages");

// these will hold the object with the value that you want, and that value:
JSONObject nestedObject = null;
int pageId = 0;

// these are the property names in the "pages" object:
String[] keys = pages.getNames(pages);

// iterate over the keys in the "pages" object, looks for JSONObjects:
for (int i = 0; i < keys.length; i++)
{
    try
    {
        nestedObject = pages.getJSONObject(keys[i]);
        // only consider objects with a "pageid" key, stop at the first one:
        if (nestedObject.has("pageid"))
            break;
    }
    catch (JSONException je)
    { ; }
}

if (nestedObject != null)
   pageId = nestedObject.getInt("pageid");

Your JSON input seems peculiar in that the first nested object has a pages field, that contains another object. The name in plural, pages, and the nested object - which duplicates the key containing the object as a pageid key within the object - suggests that pages should be an array of multiple such objects.

Upvotes: 4

Marc
Marc

Reputation: 1974

Take a look at the GSON Library: http://code.google.com/p/google-gson/

Here are good examples: https://sites.google.com/site/gson/gson-user-guide#TOC-Primitives-Examples

Gson gson = new Gson();

Map map = gson.fromJson("{\"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\"}]}}}}", HashMap.class);

Map query = (Map) map.get("query");
Map pages = (Map) query.get("pages");
System.out.println(pages.keySet());

Map page = (Map) pages.get("3037904");
System.out.println(page);
System.out.println(page.get("pageid"));

Upvotes: 0

Related Questions