简体   繁体   中英

JsonObject Get value by key

My JSON is below. I need take value by key in one loop for all objects. Current loop is wrong. I need change it. Any solutions?

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

My part of code:

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));
    }
}
for (int i = 0; i < jsonMainArr.length(); i++) {
    JSONObject objects = jsonMainArr.getJSONObject(i);
    String key = objects.getString("key");
    String value= objects.getString("value");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM