json在android应用程序解析中的问题
问题描述:
在我的应用程序中,当我点击一个url我得到一个返回数据作为json数组。它如下json在android应用程序解析中的问题
{"Status":[{ "img_path": "http://xxxxxxxxxxxxxx.com/images/thumb140/1316145577.jpg",
"img_path": "http://xxxxxxxxxxxxxx.com/images/thumb140/1316146270.jpg",
"img_path": "http://xxxxxxxxxxxxxx.com/images/thumb140/1316146473.jpg",
"img_path": "http://xxxxxxxxxxxxxx.com/images/thumb140/1316147003.jpg" } ]}
从上面的结果我想解析出的网址,我试图将它存储在数组列表中。以下是我的代码
try{
JSONArray get_post_status = json.getJSONArray("Status");
for (int i = 0; i < get_post_status.length(); i++) {
JSONObject e = get_post_status.getJSONObject(i);
if(e.equals("img_path"))
{
Get_post_image_array.add(e.getString("img_path"));
}
}
但是在我的arraylist中,我只得到结果中的最后一个url。如何获取阵列列表中的所有网址。
请帮助我.......
答
返回的JSON无效。
缩短数据,仅在结构中查找:
{
"Status":
[
{
"img_path": "a",
"img_path": "b",
"img_path": "c",
"img_path": "d"
}
]
}
我们可以看到,在阵列(括在[]
)只包含一种元素;该对象由{}
包围。该对象有几个相同密钥的实例,"img_path"
。这是无效的。您的解析器显然最终只保留最后一个实例。
的JSON应该看起来更像是这样的:
{
"Status":
[
"a",
"b",
"c",
"d"
]
}
答
你JSON是无效的。
{"Status": [ {"img_path": "blah, blah"}, {"img_path": "blah2, blah3"}]}
是你想要的。基本上,你一遍又一遍地在对象中设置相同的键,而不是创建一个对象列表。
您是否知道可以使用'example.com'作为主机名的例子? 'example.com'是一个保留的域名,如[RFC 2606](http://tools.ietf.org/html/rfc2606)中所述。 –