Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSONObject data foreach (Android)

Tags:

json

android

My following json data:

  {
        "Alex": {
            "id": "54e23a2331ac67b1490d87b8",
            "desc": "bla bla",
            "no": 2
        },
        "Kodie": {
            "id": "54e23a2331ac67b1490d87b9",
            "desc": "bla bla",
            "no": 13
        },
        "Lache": {
            "id": "54e23a2331ac67b1490d87af",
            "desc": "bla bla",
            "no": 89
        }
 }

How should the following code in the android system?

JSONObject object = new JSONObject(data);

for (int i = 0; i < object.length(); i++) {
   //desc = object.getString("desc");
}

Sorry I could tell you so much for my english is very bad.

like image 832
Thesalacium Avatar asked Feb 18 '26 14:02

Thesalacium


2 Answers

    try {
        JSONObject json = new JSONObject(data);
        Iterator<String> temp = json.keys();
        while (temp.hasNext()) {
            String key = temp.next();
            Object value = json.get(key);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
like image 123
oldfeel Avatar answered Feb 21 '26 03:02

oldfeel


Thanks @oldfell;

JSONObject object = new JSONObject(json); 
Iterator keys = object.keys(); 
while(keys.hasNext()) { 
    String dynamicKey = (String)keys.next(); 
    JSONObject line = object.getJSONObject(dynamicKey); 
    String desc = line.getString("desc"); 
} 

I solved the problem in this way

like image 39
Thesalacium Avatar answered Feb 21 '26 02:02

Thesalacium



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!