2014-09-30 97 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)); 
     } 
    } 
}