Get Key and values from JSONObject

前端 未结 3 1107
半阙折子戏
半阙折子戏 2020-12-18 02:30

I am trying to extract Key and values from my JSONObject. But i am not able to do that. Here is the JSON:

[{\"id\":[\"5\"]},{\"Tech\":[\"Java\"]}]

3条回答
  •  無奈伤痛
    2020-12-18 03:05

    Try this:

    try {
        JSONArray jsonArr = new JSONArray("[{\"id\":[\"5\"]},{\"Tech\":[\"Java\"]}]");
    
        for (int i = 0; i < jsonArr.length(); i++) {
            JSONObject jsonObj = jsonArr.getJSONObject(i);
            String k = jsonObj.keys().next();
            Log.i("Info", "Key: " + k + ", value: " + jsonObj.getString(k));
        }
    
    } catch (JSONException ex) {
        ex.printStackTrace();
    }
    

提交回复
热议问题