我想解析這個(gè) 并使用該數(shù)據(jù)生成Map< String,HashSet< String>>. 基本上我想要逆轉(zhuǎn)this程序. 這個(gè)項(xiàng)目的所有代碼都可以在on my github page here找到,它很短. 更新 解決方法: 使用 更新: 使用TypeToken 或者你可以解析它…… >創(chuàng)建一個(gè) File f = new File("/home/matthias/Workbench/SUTD/nytimes_corpus/wdtk-parent/wdtk-examples/JSON_Output/user.json"); String jsonTxt = null; if (f.exists()) { InputStream is = new FileInputStream("/home/matthias/Workbench/SUTD/nytimes_corpus/wdtk-parent/wdtk-examples/JSON_Output/user.json"); jsonTxt = IOUtils.toString(is); } //System.out.println(jsonTxt); Gson gson=new Gson(); Map<String, HashSet<String>> map = new HashMap<String, HashSet<String>>(); map=(Map<String, HashSet<String>>) gson.fromJson(jsonTxt, map.getClass()); //// \\ // ! PRINT IT ! // \\ // \\ // \\ // \\ // \\ // \ for (Map.Entry<String, HashSet<String>> entry : map.entrySet()) { System.out.println(entry.getKey() " : " Arrays.deepToString(map.entrySet().toArray()) ); }
Gson
Gson gson = new Gson(); String json = "<YOUR_JSON_STRING_HERE>";Map<String, HashSet<String>> map = new HashMap<String, HashSet<String>>();map = (Map<String, HashSet<String>>) gson.fromJson(json, map.getClass());
Type type = new TypeToken<Map<String, HashSet<String>>>(){}.getType();map = (Map<String, HashSet<String>>) gson.fromJson(json, type);
JSONObject
的對(duì)象
>創(chuàng)建HashMap的對(duì)象
>迭代jsonObj.keys()并為每個(gè)鍵獲取值
jsonObj.getString(鍵).
>將它放在map.put(key,value)中.
聯(lián)系客服