JsonObject按键获取值

时间:2016-08-11 08:32:40

标签: java android json

我的JSON如下。我需要在一个循环中为所有对象获取值。当前循环错误。我需要改变它。任何解决方案?

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

我的代码部分:

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));
    }
}

1 个答案:

答案 0 :(得分:-1)

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