【发布时间】:2017-03-24 08:23:04
【问题描述】:
我尝试解析服务器返回给我的应用程序的多个 JSON 数组。
服务器返回的消息结构如下:
[json 对象][json 对象]
返回消息:
[{"latitude":"44.33","longitude":"44.33","name":"test1","Notification":"true"}][{"latitude":"44.33","longitude ":"44.33","name":"test2","Notification":"false"}]
现在我试图解析这个并获取通知状态(真/假),但我没有得到我需要的项目。
try {
JSONArray jr = new JSONArray(answer);
JSONObject jb = (JSONObject)jr.getJSONObject(0);
JSONArray nof = jb.getJSONArray("Notification");
Log.d("Json", String.valueOf(nof));
}catch(Exception e)
{
e.printStackTrace();
}
如果有人可以帮助我了解我需要做些什么来实现我的使命,我会很高兴。
【问题讨论】:
-
你可以使用 GSON 进行解析,它更容易。使用 GsonFormatter Android studio 插件创建模型类
-
第一行的答案是什么?你有什么错误吗?
-
是的,我收到下一个错误 - “java.lang.String 类型的通知中的值 true 无法转换为 JSONArray”
-
您的返回消息不是有效的 JSON。尝试用一个键将这些数组包装在一个数组中。像这样:hastebin.com/amohisened.json
-
您应该使用
String nof = jb.getString("Notification");而不是JSONArray nof = jb.getJSONArray("Notification");并打印您可以简单地打印nof 的值,例如Log.d("Json", nof);