java 如何使用 org-json 库解析 json?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6192553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to parse json, using org-json libraries?
提问by James Raitsev
Looking for org-json solutions only please **
请只寻找 org-json 解决方案 **
Suppose you deal with a structure, as follows.
假设您处理一个结构,如下所示。
Using json-org
, how can i get an array of Tests, if this entire json is represented in a String
s?
使用json-org
,如果整个 json 以String
s表示,我如何获得一组测试?
Using Google's gson, it's easy to do by testing what type given object is ... i am missing something simple here with json-org
libraries
使用谷歌的 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
}
}
回答by James Raitsev
It so happens i got it before someone else had a chance to help. In case someone else has similar question, i am posting an answer below:
碰巧我在别人有机会帮助之前就得到了它。如果其他人有类似的问题,我在下面发布答案:
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));
}