2012-08-13 127 views
0
[ 
    { 
    "countries": [ 
     { 
     "id": 1, 
     "country": "India" 
     }, 
     { 
     "id": 2, 
     "country": "Australia" 
     }, 
     { 
     "id": 3, 
     "country": "Srilanka" 
     }, 
     { 
     "id": 4, 
     "country": "Pakistan" 
     }, 
     { 
     "id": 5, 
     "country": "Switzerland" 
     } 
    ] 
    } 
] 

如何在Android中解析此Json响应。我没有得到任何适当的解决方案。 请帮助我。Android Json解析复杂阵列

+0

参见[这](http://www.androidhive.info/2012/01/android-json-parsing-tutorial/ )教程。它可以帮助你。 – Praveenkumar 2012-08-13 04:24:54

+0

你需要有任何名称的数组。你没有提到它。 – Harish 2012-08-13 04:29:57

回答

4

做这样的......

JSONArray arr = locs.getJSONArray("countries"); 


for (int i = 0; i < arr.length(); ++i) { 
    JSONObject rec = arr.getJSONObject(i); 
    int id = rec.getInt("id"); 
    String con = rec.getString("country"); 
    // ... 
} 
1
JSONArray jArr=new JSONArray(response); 
    for(int i=0;i<jArr.length;i++) 
    { 
    id[]=jArr.getJSONObject(i).getInt("id"); 
    con[]=jArr.getJSONObject(i).getString("country"); 
    } 
0
JSONArray countriesobj = new JSONArray(); 
JSONObject json; 
      try { 
       JSONObject jObject = new JSONObject(response); 
       json = jObject; 

       countriesobj = json.getJSONArray("countries"); 

       country = new String[countriesobj.length()]; 
       for (int i = 0; i < countriesobj.length(); i++) { 

        JSONObject e = countriesobj.getJSONObject(i); 

        country[i] = e.getString("country"); 

       } 

      } catch (Exception e) { 
       e.printStackTrace(); 

      }