2016-10-13 8 views
0

ich zur Zeit die folgende JSON Struktur haben:Wie Schleife durch JSONArray innerhalb JSONObject

{ 
    "Apps": [ 
    { 
     "column1": "sample string 1", 
     "column2": true 
    }, 
    { 
     "column1": "sample string 1", 
     "column2": true 
    } 
    ], 
    "param": true 
} 

Wie erhalte ich die Werte der column1 und column2? Was ich nur parsen kann, ist ein JSONObject in einem JSONArray.

Antwort

0

So etwas wie das.

// Get a reference to the JSON object 
JSONObject jSONObject = new JSONObject(stringJsonResponse); 
// Getting the JSON array node 
JSONArray jsonAray = jSONObject.getJSONArray("Apps"); 
// Looping through the json array 
for (int i = 0; i < jsonArray.length(); i++) { 
    JSONObject childrenObject = childrenArray.getJSONObject(i); 
    ... 
    ... 
    ... 
} 

Sie können einen Blick auf, wie ich JSON-Daten analysiert, wenn ich ähnliche Daten empfangen https://github.com/gSrikar/AskReddit/blob/master/app/src/main/java/com/example/srikar/askreddit/MainActivity.java

1
JSONObject jSONObject = new JSONObject(yourStrinig); 
JSONArray jArray = jSONObject.getJSONArray("Apps"); 
for (int i = 0; i < jArray.length(); i++) { 
    JSONObject childrenObject = jArray.getJSONObject(i); 
    String column1 = childrenObject.getString("column1"); 
    String column2 = childrenObject.getString("column2"); 
} 
1

Mit diesem können Sie eine Schleife durch alle Elemente

private void showJSON(String response){ 
       String name=""; 
       String phone_number=""; 
       try { 
        JSONObject jsonObject =new JSONObject(response); 
        JSONArray result = jsonObject.getJSONArray(config.JSON_ARRAY); 
        for(int x=0;x<result.length();x++) { 
         JSONObject collegeData = result.getJSONObject(x); 
         name = collegeData.getString(config.KEY_NAME); 
         phone_number = collegeData.getString(config.KEY_PHONE_NUMBER); 
         //you can save your data in list here 
        } 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 

      } 
Verwandte Themen