简体   繁体   中英

json array to individual strings

Assignment: I am using json-simple. How can I convert this json data into individual java strings? (Please forgive me if you think that this is a low-level question - I am new to JSON, so I don't know much about that - I've searched a lot, but I couldn't find any answers)


I can get the data if there is only one object ... like this ...

{
    "name": "Abhi",
    "age": "21"
}

But, I can't get the data if it is in the array

[{
    "name": "Abhi",
    "age": "21"
}, {
    "name": "shek",
    "age": "7"
}]

my program logic for json object

    JSONParser parser = new JSONParser();

    Object obj = parser.parse(new FileReader("A:/c/dataFile.json"));

    JSONObject jObj = (JSONObject) obj;

    String gName = (String) jObj.get("name");
    String gAge = (String) jObj.get("age");
    System.out.println(gName);
    System.out.println(gAge);

Can anyone show me how to get the data? maybe a code snippet?

Thanks in advance for your answer!

Because in your second case you are getting JSONArray you may need to check the instance of obj as

if (jObj instanceof JSONObject)

else if (jObj instanceof JSONArray)

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