获取json数组值android

时间:2014-09-30 13:16:28

标签: android json

我有一个关于如何解析json文件的问题。我的json结构是这样的:

{
    "contacts": [
        {
                "id": "c200",
                "name": "Ravi Tamada",
                "email": "ravi@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "services": [
                   "laundry",
                   "wifi",
                   "tv",
                   "swimming pool",
                   "bar"
                 ],
                "phone": [
                    910000000000,
                    00000000,
                    000000
                ]
        },
        {
                "id": "c201",
                "name": "Johnny Depp",
                "email": "johnny_depp@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "services": [
                   "laundry",
                   "wifi",
                   "tv",
                   "swimming pool",
                   "bar"
                 ],
                "phone": [
                    0000000000,
                    00000000,
                    00000000
                ]
        }
  ]

如何获取电话价值和服务价值?

phones = jsonObj.getJSONArray(TAG_PHONE);
for (int x = 0; x < phones.length(); x++) {

}

因为以ID为例我没有遇到问题:

for (int i = 0; i < contacts.length(); i++) {
                        JSONObject c = contacts.getJSONObject(i);

                        String id = c.getString(TAG_ID);
// tmp hashmap for single contact
                        HashMap<String, String> contact = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        contact.put(TAG_ID, id);
contactList.add(contact);

非常感谢

2 个答案:

答案 0 :(得分:1)

PhoneServices是JSONArray对象,所以当你执行get函数时,你应该使用.getJSONArray()

例如:

JSONArray phoneArray = c.getJSONArray("phone");
for(int i=0;i<phoneArray.length();i++){
    JSONObject json_phone_data = phoneArray.getJSONObject(i);
    String phone_data = phoneArray.getString(i);
    // Do something with phone data
}

JSONArray servicesArray = c.getJSONArray("services");
for(int i=0;i<servicesArray.length();i++){
    JSONObject json_services_data = servicesArray.getJSONObject(i);
    String services_data = servicesArray.getString(i);
    // Do something with services data
}

请参阅documentation

答案 1 :(得分:1)

服务和电话是联系人的内部JSONArray。因此,从联系人JSONObject,您可以使用他们的密钥来检索相应的对象并循环

for (int i = 0; i < contacts.length(); i++) {
    JSONObject c = contacts.getJSONObject(i);
    JSONArray phone = c.optJSONArray("phone")
    if (phone != null) {
       for (int x = 0; x < phones.length(); x++) {
            Log.i("PHONE", "phone at #" + x + " " + phone.optInt(x));
        }   
    }

    JSONArray services = c.optJSONArray("services"); 
    if (services != null) {
      for (int j = 0; j < services.length(); j++) {
            Log.i("SERVICE", "service at #" + j + " " + services.optString(j));
      } 
    }
}