2011-05-31 126 views
3

寻找组织JSON的解决方案,只请**如何使用org-json库解析json?

假设你处理的结构,如下所示。

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

使用谷歌的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 
    } 
} 

回答

7

恰巧我得到它之前别人有机会帮助。如果别人有类似的问题,我发布以下答案:

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

JSONObject o = new JSONObject(s);在这一行我得到了eroor。 “构造函数JSONObject未定义”。 – 2011-06-18 09:43:52

+1

你的情况是什么?应该是String对象,我相信 – JAM 2011-06-18 15:11:50