2014-09-30 83 views
0

我有一个关于如何解析json文件的问题。我的json结构是这样的:获取json数组值android

{ 
    "contacts": [ 
     { 
       "id": "c200", 
       "name": "Ravi Tamada", 
       "email": "[email protected]", 
       "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": "[email protected]", 
       "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我haven't有问题:

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); 

非常感谢您

+0

复制到检索相应的对象和环接触前请交 http://stackoverflow.com/questions/19197015/android-parse-json-array检查字符串 – 2014-09-30 13:19:17

+0

参考这[链接](http://stackoverflow.com/questions/14708935/how-to-get-json-array-values-in-android) – Yugesh 2014-09-30 13:22:30

回答

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

+0

非常感谢,我怎么能json_data i元素的字符串值?谢谢:) – 2014-09-30 13:28:39

+0

那么如果你想要的字符串,你可以简单地使用'字符串json_phone_data = phoneArray.getString(i);' – erad 2014-09-30 13:31:44

+0

并在列表中适配器我怎么能做到这一点? TextView phone =(TextView)vi.findViewById(R.id.telefono_alojamiento); HashMap contact = new HashMap (); contact = data.get(position); phone.setText(song.get(MyFragment.TAG_PHONE))); – 2014-09-30 14:44:47

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)); 
     } 
    } 
}