Parse JSON object with string and value only

后端 未结 2 1892
灰色年华
灰色年华 2020-12-01 01:49

I have problem when trying to parse with minimum value to map in Android.

There some sample JSON format with more information ex:

[{id:\"1\", name:\"         


        
2条回答
  •  有刺的猬
    2020-12-01 02:39

    You need to get a list of all the keys, loop over them and add them to your map as shown in the example below:

        String s = "{menu:{\"1\":\"sql\", \"2\":\"android\", \"3\":\"mvc\"}}";
        JSONObject jObject  = new JSONObject(s);
        JSONObject  menu = jObject.getJSONObject("menu");
    
        Map map = new HashMap();
        Iterator iter = menu.keys();
        while(iter.hasNext()){
            String key = (String)iter.next();
            String value = menu.getString(key);
            map.put(key,value);
        }
    

提交回复
热议问题