简体   繁体   中英

How to obtain the content from Multi-Level JSON format in JAVA?

For example, a kind of JSON as below:

    { "x":"1","y":"2","z":{"a":"1","b":"2","c":"3"}}

Put this as string in JSONObject argument:

    JSONObject jaob=new JSONObject(xxx)

and from method "get("x")" of JSONObject I can obtain the value "1"

     jaob.get("x")

But how to get "a" of the second level JSON format "z"???

When I try to obtain by

     JSONObject(jaob.get("z").toString()).get("a")

but it doesn't work.
Does any one have the idea?
Any response is appreciated, thanks

jaob.getJSONObject("Z").getString("a")

alternatively, you could use getLong or getString on a .

If you read the javadocs it's pretty easy stuff. The reason yours didn't work is that get returns a java.lang.Object not a JSONObject or JSONArray .

Have you tried

JSONObject jaob = new JSONObject(xxx);

jaob.getJSONArray("z");

//or 

jaob.getJSONObject("z");

they both return JSONObject according to JSONObject

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM