如何提取我的 simplejson 数组的数据?

How to extract data of my simplejson array?

我想提取我的 JSONArray 对象的所有注释,我找到了这个 post https://www.mkyong.com/java/json-simple-example-read-and-write-json/。问题是我的 JSONArray 是一种 "array of array":

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

我试过这段代码(来自我链接的 post)

 System.out.println(annotationJSON.toJSONString());

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

结果是 "null",我认为这是因为我需要进入数组 "annotation" 而目前我只在 "AnnotationOfzelda"

我找到了一种方法,即使它看起来不太好看

System.out.println(annotationJSON.toJSONString());
JSONObject tmp = (JSONObject) annotationJSON.get(0);
System.out.println("......");
JSONArray tmpArr = (JSONArray) tmp.get("AnnotationOfzelda");
for (int i = 0; i < tmpArr.size(); i++) {
 tmp = (JSONObject) tmpArr.get(i);
 System.out.println(tmp.get("annotation"));
}

尝试如下:

 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) ;
         }

    }

试试这个,你可以将它用于 JSON 中的特定值(注释:"asd")或完整的 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) {
                    System.out.println(node.get("annotation"));
                    continue;
                }
                if (node.iterator().hasNext()) {
                    iterator = node.iterator();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

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