I want to extract all the annotation of my JSONArray object and I found this post https://www.mkyong.com/java/json-simple-example-read-and-write-json/. The problem is that my JSONArray is a kind of "array of array":


I tried this code (from the post I linked)


 Iterator<JSONObject> iterator = annotationJSON.iterator();
 while (iterator.hasNext()) {
  JSONObject factObj = (JSONObject) iterator.next();
  String annotation = (String) factObj.get("annotation");

And the result is "null" and I think it's because I need to go in the array "annotation" and at the moment I'm only in "AnnotationOfzelda"

3 Answers

Community On Best Solutions

Try this you can use it for specific value in the JSON (annotation:"asd") or for full JSON:

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.Iterator;

public class Sol {
    public static void main(String[] args) {
        String data = "[{\"AnnotationOfzelda\":\n" +
                " [{\"annotation\":\n" +
                "  [{\"duration\":5000,\"annotation\":\"salut\",\"timestamp\":2250.0}, \n" +
                "   {\"duration\":5000,\"annotation\":\"jp\",\"timestamp\":4570.0}]}, \n" +
                "   {\"duration\":5000,\"annotation\":\"asd\",\"timestamp\":3340.0}, \n" +
                "   {\"duration\":5000,\"annotation\":\"asd\",\"timestamp\":4040.0}]}]";
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonNode = null;
        try {
            jsonNode = objectMapper.readTree(data);
            Iterator iterator = jsonNode.iterator();
            while (iterator.hasNext()) {
                JsonNode node = (JsonNode) iterator.next();
                if (node.get("annotation") != null) {
                if (node.iterator().hasNext()) {
                    iterator = node.iterator();
        } catch (IOException e) {

Output [{"duration":5000,"annotation":"salut","timestamp":2250.0},{"duration":5000,"annotation":"jp","timestamp":4570.0}] "asd" "asd"

J.erome On

I found a way to do it even if it's not really nice looking

JSONObject tmp = (JSONObject) annotationJSON.get(0);
JSONArray tmpArr = (JSONArray) tmp.get("AnnotationOfzelda");
for (int i = 0; i < tmpArr.size(); i++) {
 tmp = (JSONObject) tmpArr.get(i);

Swati On

Try like below :

 JSONArray jsonArray = annotationJSON.getJSONArray("AnnotationOfzelda
    ");//getting array
     for (int i = 0; i < jsonArray.length(); i++) {
      JSONObject jsonobject= jsonArray.getJSONObject(i);//getting first element 
       String duration1= jsonobject.getString("duration");//getting duration
              String annotation1= jsonobject.getString("annotation");//getting annotation1
               String timestamp1= jsonobject.getString("timestamp");//getting timestamp
     JSONArray jsonObject1= jsonobject.getJSONArray("annotation"); //getting children array
      for (int j = 0; j < jsonObject1.length(); j++) {
             JSONObject object1 = jsonObject1.getJSONObject(j);
             String duration= object1.getString("duration");//getting duration

              String duration= object1.getString("annotation");//getting annotation
               String duration= object1.getString("timestamp");//getting timestamp
          System.out.println(""+duration+""annotation""+timestamp) ;