MarsPeople
MarsPeople

Reputation: 1824

Class:JSONParser get values by id?

My class:

import org.json.simple.parser.JSONParser

My json text

{"18302":{"ns":0,"rev":[{"user":"ABC"}],"title":"MyTitle"}}

How can i get the values of "the key that i don't know what comes from api"?

I want to get like this?

JSONObject jsonObject = (JSONObject) new JSONParser().parse(myJsonText);
String value = jsonObject.get(0);

Can i get value with id? Or how can i get the value without "key name"?

Upvotes: 1

Views: 806

Answers (1)

Appak
Appak

Reputation: 492

You can get all the keys as a set.

JSONObject obj = (JSONObject)JSONValue.parse(jsonString);
for (Object key : obj.keySet()) {
    Object value = obj.get(key);
    System.out.printf("key %s(%s)\nvalue %s(%s)\n\n",
           key.getClass().getSimpleName(),
           key,
           value.getClass().getSimpleName(),
           value);
}

I recommend you download the full release for the version you are using that way you get the javadoc too. https://code.google.com/p/json-simple/downloads/list

Upvotes: 2

Related Questions