从java中的json字符串创建哈希图?
我有喜欢的json字符串{"phonetype":"N95","cat":"WP"},想要转换为标准的Hashmap。
{"phonetype":"N95","cat":"WP"}
我该怎么做?
解析JSONObject并创建HashMap
public static void jsonToMap(String t) throws JSONException { HashMap<String, String> map = new HashMap<String, String>(); JSONObject jObject = new JSONObject(t); Iterator<?> keys = jObject.keys(); while( keys.hasNext() ){ String key = (String)keys.next(); String value = jObject.getString(key); map.put(key, value); } System.out.println("json : "+jObject); System.out.println("map : "+map); }
测试输出:
json : {"phonetype":"N95","cat":"WP"} map : {cat=WP, phonetype=N95}