在Android应用程序中的json解析中的问题

时间:2011-09-19 15:23:03

标签: android json parsing

在我的应用程序中,当我点击一个网址时,我得到一个返回数据作为json数组。它如下

{"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。如何获得arraylist中的所有网址。

请帮助我.......

2 个答案:

答案 0 :(得分:3)

返回的JSON无效。

缩短数据,仅查看结构:

{
    "Status":
        [
            {
                "img_path": "a", 
                "img_path": "b", 
                "img_path": "c", 
                "img_path": "d"
            }
        ]
}

我们可以看到数组(包含在[]中)只包含一个元素;由{}括起来的对象。此对象具有相同密钥"img_path"的多个实例。这是无效的。您的解析器显然最终只保留最后一个实例。

JSON应该看起来更像这样:

{
    "Status":
        [
            "a", 
            "b", 
            "c", 
            "d"
        ]
}

答案 1 :(得分:2)

您的JSON无效。

{"Status": [ {"img_path": "blah, blah"}, {"img_path": "blah2, blah3"}]}

是你想要的。实质上,您一遍又一遍地在对象中设置相同的键,而不是创建对象列表。

相关问题