在android中解析jsonarray

时间:2012-04-11 06:49:11

标签: android json parsing

我需要从下面的json

解析json数组联系人
{

    },
    //Truncated for clarity
   ]
}

3 个答案:

答案 0 :(得分:4)

解析很容易。首先获取json-simple并将其添加到您的项目中(或者您可以使用内置的JSON库,如果您喜欢该库中的所有内容都可以抛出已检查的JSONException)。

接下来,您的整个响应文本是一个有效的JSON对象。所以:

JSONObject response = (JSONObject)JSONValue.parse(responseText);

您的对象在“result”键下有一系列结果。你可以这样:

JSONArray results = (JSONArray)response.get("result");

您的数组包含一组要迭代的对象,以获取每个对象的联系信息。例如:

for (Object obj : results) {
    JSONObject entry = (JSONObject)obj;
    JSONArray contacts = (JSONArray)entry.get("contact");
    for (Object contact : contacts) {
        System.out.println(contact);
    }
}

请注意,将JSON放在一起的人决定使用数组类型来保存每个联系人条目。对象类型会更有意义,但按原样,contact.get(0)会为您提供联系人条目的类型代码(tw等),以及contact.get(1)将为您提供实际的联系电话/地址/等等。

答案 1 :(得分:1)

我认为这不是一个有效的Json。普通的Json必须有"Label""value"。标签和值必须用冒号:

分隔
"w","www.firmdale.com"

应该是这样的

"w":"www.firmdale.com"

http://code.google.com/p/jsonvalidator/

编辑:根据 Aroth

更清晰明确
object
    {}
    { members } 
members
    pair
    pair , members
pair
    string : value
array
    []
    [ elements ]
elements
    value
    value , elements
value
    string
    number
    object
    array
    true
    false
    null 

答案 2 :(得分:0)

 private JSONObject jObject;
 jObject = new JSONObject(your response as string);
 //this will give you json object for HotelInformationResponse
 JSONArray menuitemArray = popupObject.getJSONArray("result"); 
 // loop through this array to get all values 

了解更多信息Check this link

相关问题