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

时间:2019-05-03 12:16:42

标签: java json simplejson

我想提取我的JSONArray对象的所有注释,并且发现了这篇文章https://www.mkyong.com/java/json-simple-example-read-and-write-json/。问题是我的JSONArray是一种“数组数组”:

[{"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}]}]

我尝试了这段代码(来自我链接的帖子)

 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”

3 个答案:

答案 0 :(得分:2)

尝试如下:

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

    }

答案 1 :(得分:1)

尝试此操作,您可以将其用于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”

答案 2 :(得分:0)

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

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