0

我需要从下面的 json 解析 json 数组联系人

{

    },
    //Truncated for clarity
   ]
}
4

3 回答 3

4

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

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

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

您的对象在“结果”键下有一组结果。你可以得到这样的:

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)为您提供实际的联系电话/地址/其他。

于 2012-04-11T07:08:52.683 回答
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 
于 2012-04-11T07:10:27.633 回答
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 

欲了解更多信息,请查看此链接

于 2012-04-11T06:59:52.740 回答