无法使用 json 对象读取 json 数组,NPE

Cant read jsonarray with json object, NPE

我是 JSON 格式和 Java 的新手。如果我遗漏了一点请告诉我。

这是我的解析器代码。路径是真的。如果我写的话,我可以阅读 blabla 部分。但是 examples.size 始终为空。所以我无法阅读文件的其余部分。 我用json简单1.1.1

   `JSONParser parser = new JSONParser();
        Object obj = parser.parse(path);
        JSONObject jsonObject = (JSONObject) obj;
        JSONArray examples= (JSONArray) jsonObject.get("example");
        System.out.println("Example SIZE:" + examples.size());`

我无法分享我的确切 json 文件,但这与我的 JSON:

格式相似
{
  "examples":{
    "blabla":"blabla",
    "example":[
      {
        "id":"1",
        "firstName":"Leonardo",
        "lastName":"DiCaprio",
        "photo":"http://1.bp.blogspot.com/-zvS_6Q1IzR8/T5l6qvnRmcI/AAAAAAAABcc/HXO7HDEJKo0/s200/Leonardo+Dicaprio7.jpg",
        "movieandpoint":[
          {
            "movie":"Inception",
            "point":"9.1"
          },
          {
            "movie":"Catch me if you can",
            "point":"8"
          }
        ],
        "decision":{
          "user":"admin",
          "#text":"ok"
        },
        "movieandpoint#1":[
          {
            "movie":"another one",
            "point":"7"
          }
        ]
      },
      {
        "id":"2",
        "firstName":"Johnny",
        "lastName":"Depp",
        "photo":"http://4.bp.blogspot.com/_xR71w9-qx9E/SrAz--pu0MI/AAAAAAAAC38/2ZP28rVEFKc/s200/johnny-depp-pirates.jpg",
        "movieandpoint":[
          {
            "movie":"Sweeny Tod",
            "point":"9"
          },
          {
            "movie":"Yoga Hosers",
            "point":"5"
          }
        ],
        "decision":{
          "user":"admin",
          "#text":"ok"
        },
        "movieandpoint#1":[
          {
            "movie":"another",
            "point":"9"
          }
        ]
      }
    ]
  }
}

您必须阅读 parent 到 child。像下面这样的东西会起作用。首先,您必须阅读“示例”jsonobject,然后是 child“示例”。顺便说一下,我无法 运行 使用您的 json 文件,就像其他用户在评论中提到的那样,json 文件在结构上不正确。

JSONParser parser = new JSONParser();
    Object obj = parser.parse(aa);
    org.json.simple.JSONObject jsonObject = (org.json.simple.JSONObject) obj;
    org.json.simple.JSONObject object1 = (org.json.simple.JSONObject) jsonObject.get("examples");
   JSONArray examples = (JSONArray) object1.get("example");
    System.out.println("Example SIZE:" + examples.size());