Home > database >  How to retrieve json array elements from JSON object in Java?
How to retrieve json array elements from JSON object in Java?

Time:06-23

JSONObject firstObject = (JSONObject) jsonParser.parse(new FileReader(firstNamesPath));

I have this JSONObject, and I want to be able to access elements in the array inside of it. The object opens successfully, I just don't know how to access the array called "firstNames". It is in a file, and the object looks like this.

{
     "firstNames": [ 
          "Aaron",
          "Abigail",
          "Albert",
          "Bob"
     ]
}

Edit: I am using org.json.simple.JSONObject . If this is not recommended, I am more than willing to change it.

CodePudding user response:

There are several ways to retrieve the json array value:

Assume we have a jsonString

jsonString = "{\n"   "     \"firstNames\": [ \n"   "          \"Aaron\",\n"   "          \"Abigail\",\n"   "          \"Albert\",\n"   "          \"Bob\"\n"   "     ]\n"   "}";

(since many classes share similar names, I am using the groupId and artifactId for distinction.)

Simple cases: use generic JSONObjects and JSONArrays.

json-simple (which OP is using) json-simple website, maven :

org.json.simple.parser.JSONParser jsonParser = new org.json.simple.parser.JSONParser();
org.json.simple.JSONObject firstObject = (org.json.simple.JSONObject) jsonParser.parse(jsonString);
org.json.simple.JSONArray jsonArray = (org.json.simple.JSONArray) firstObject.get("firstNames");
System.out.println(jsonArray);

JSON in Java (mentioned in adendrata's answer): JSON-Java doc, maven

org.json.JSONObject secondObject = new org.json.JSONObject(jsonString);
org.json.JSONArray jsonArray2 = secondObject.getJSONArray("firstNames");
System.out.println(jsonArray2);

gson: Gson, maven

com.google.gson.JsonObject thirdObject = com.google.gson.JsonParser.parseString(jsonString).getAsJsonObject();
System.out.println(thirdObject.get("firstNames").getAsJsonArray());

For more complicated use cases, if you'd like to define your own class, and want to deserialize JSON string to your class, then you can use Gson or Jackson:


// Create your own class:
/*
public class YourOwnClass {
    private List<String> firstNames;

    public List<String> getFirstNames() {
        return firstNames;
    }
}
*/

Gson gson = new Gson();
YourOwnClass customObject1 = gson.fromJson(jsonString, YourOwnClass.class);
System.out.println(customObject1.getFirstNames());

ObjectMapper mapper = new ObjectMapper();
YourOwnClass customObject2 = mapper.readValue(jsonString, YourOwnClass.class);
System.out.println(customObject2.getFirstNames());

CodePudding user response:

you can use JSONArray to get array type of Json and looping to access each index

example:

    JSONArray array = firstObject.getJSONArray("firstNames");
    for (int i = 0; i < array.length(); i  ) {
        System.out.println("Hello i'm "   array.get(i));
    }

CodePudding user response:

Try to use this : com.alibaba.fastjson.JSONObject, and here's the dependency:

    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.41</version>
    </dependency>

Then you can directly use the getJSONArray method the answer shown above.

  • Related