2016-08-11 2 views
2

Mein JSON ist unten. Ich brauche Wert, indem ich für alle Objekte eine Schleife einfüge. Stromschleife ist falsch. Ich muss es ändern. Irgendwelche Lösungen?JsonObject Holen Wert nach Schlüssel

[ 
    { 
    "key": "firstName", 
    "value": "8796ccd6-56c8-441f-830b-bb1caa16d3d1" 
    }, 
    { 
    "key": "lastName", 
    "value": "92e68944-e54c-4ad3-b368-cc5b35dcf9f6" 
    }, 
    { 
    "key": "user", 
    "value": "0d866084-78aa-4fcf-a110-7627ce831172" 
    } 
] 

Mein Teil des Codes:

for (int i = 0; i < jsonMainArr.length(); i++) { 
    JSONObject objects = jsonMainArr.getJSONObject(i); 
    Iterator key = objects.keys(); 
    while (key.hasNext()) { 
     String k = key.next().toString(); 
     System.out.println("Key : " + k + ", value : " + objects.getString(k)); 
    } 
} 

Antwort

-1
for (int i = 0; i < jsonMainArr.length(); i++) { 
    JSONObject objects = jsonMainArr.getJSONObject(i); 
    String key = objects.getString("key"); 
    String value= objects.getString("value"); 
} 
+0

aber es wird dauern JSONObject erste, zweite usw. brauche ich alle Werte in einer Schleife erhalten –

-1
jObject = new JSONObject(contents.trim()); 
Iterator<?> keys = jObject.keys(); 

while(keys.hasNext()) { 
    String key = (String)keys.next(); 
    if (jObject.get(key) instanceof JSONObject) { 

    } 
} 
+1

Bitte um eine Erklärung für Ihren Code zur Verfügung stellen um anderen Nutzern zu helfen, es besser zu verstehen. – OldMcDonald

Verwandte Themen