Parsing JSON string in android

后端 未结 2 640
迷失自我
迷失自我 2020-12-07 02:19

Parsing JSON seems like it is a pretty common topic of discussion on here. I have looked around and still have not found what I am looking for. Here is my code for my HttpC

2条回答
  •  北荒
    北荒 (楼主)
    2020-12-07 02:26

    In this case you can use the keys method of the JSONObject class. It will basically returns an Iterator of the keys, that you can then iterate to get and put the values in a map:

    try {
        JSONObject jsonObject = new JSONObject(theJsonString);
        Iterator keys = jsonObject.keys();
        Map map = new HashMap();
        while (keys.hasNext()) {
            String key = (String) keys.next();
            map.put(key, jsonObject.getString(key));
        }
        System.out.println(map);// this map will contain your json stuff
    } catch (JSONException e) {
        e.printStackTrace();
    }
    

提交回复
热议问题