如何使用org-json库解析json?

Jam*_*sev 4 java parsing json

只寻找org-json解决方案**

假设您处理结构,如下所示.

使用json-org,如果整个json用Strings 表示,我如何得到一个测试数组?

使用谷歌的gson,通过测试给定对象的类型很容易做到...我在这里缺少一些简单的json-org

{
   "Groups":{
      "Test":[
         {
            "Test Number":123456,
            "Channel":"TEST",
            "environment":"A",
            "output event":[
               {
                  "description":"very good description",
                  "value":123,
                  "active":true
               },
               {
                  "description":"another very good description",
                  "value":456,
                  "active":true
               }
            ],
            "active":true,
            "instrument":"ABC"
         },
         {
            "Test Number":547985,
            "Channel":"some new channel",
            "environment":"B",
            "output event":[
               {
                  "description":"reject",
                  "value":123,
                  "active":true
               },
               {
                  "description":"ack",
                  "value":456,
                  "active":true
               }
            ],
            "active":true,
            "instrument":"XYZ"
         }
      ],
      "name":"A clever name",
      "active":true
   }
}
Run Code Online (Sandbox Code Playgroud)

Jam*_*sev 8

在其他人有机会提供帮助之前我就得到了它.如果其他人有类似问题,我将在下面发布一个答案:

JSONObject o = new JSONObject(s);

JSONArray arrayOfTests = (JSONArray) ((JSONObject) o.get("Groups")).get("Test");

for (int i = 0; i < arrayOfTests.length(); i++) {
    System.out.println(arrayOfTests.get(i));
}
Run Code Online (Sandbox Code Playgroud)

  • JSONObject o = new JSONObject(s); 在这一行我得到了eroor."构造函数JSONObject未定义". (2认同)