【发布时间】:2012-04-16 19:31:12
【问题描述】:
我有一个 json 流,它可以是:
{"intervention":
{
"id":"3",
"subject":"dddd",
"details":"dddd",
"beginDate":"2012-03-08T00:00:00+01:00",
"endDate":"2012-03-18T00:00:00+01:00",
"campus":
{
"id":"2",
"name":"paris"
}
}
}
或类似的东西
{"intervention":
[{
"id":"1",
"subject":"android",
"details":"test",
"beginDate":"2012-03-26T00:00:00+02:00",
"endDate":"2012-04-09T00:00:00+02:00",
"campus":{
"id":"1",
"name":"lille"
}
},
{
"id":"2",
"subject":"lozlzozlo",
"details":"xxx",
"beginDate":"2012-03-14T00:00:00+01:00",
"endDate":"2012-03-18T00:00:00+01:00",
"campus":{
"id":"1",
"name":"lille"
}
}]
}
在我的 Java 代码中,我执行以下操作:
JSONObject json = RestManager.getJSONfromURL(myuri); // retrieve the entire json stream
JSONArray interventionJsonArray = json.getJSONArray("intervention");
在第一种情况下,上述方法不起作用,因为流中只有一个元素..
如何检查流是object 还是array?
我尝试使用json.length(),但没有成功..
谢谢
【问题讨论】:
-
请检查下面的链接以找到更好的结果developer.android.com/reference/org/json/JSONTokener#nextValue