java - How to parse JSON object into `Map<String, HashSet<String>>` -


i'd parse json object:

"{   \"rao\":[\"q7293658\",\"\",\"q7293657\",\"q12953055\",\"q3531237\",\"q4178159\",\"q1138810\",\"q579515\",\"q3365064\",\"q7293664\",\"q1133815\"],   \"hani durzy\":[\"\"],   \"louise\":[\"\",\"q1660645\",\"q130413\",\"q3215140\",\"q152779\",\"q233203\",\"q7871343\",\"q232402\",\"q82547\",\"q286488\",\"q156723\",\"q3263649\",\"q456386\",\"q233192\",\"q14714149\",\"q12125864\",\"q57669\",\"q168667\",\"q141410\",\"q166028\"],   \"reyna\":[\"q7573462\",\"q2892895\",\"q363257\",\"q151944\",\"q3740321\",\"q2857439\",\"q1453358\",\"q7319529\",\"q733716\",\"q16151941\",\"q7159448\",\"q5484172\",\"q6074271\",\"q1753185\",\"q7319532\",\"q5171205\",\"q3183869\",\"q1818527\",\"q251862\",\"q3840414\",\"q5271282\",\"q5606181\"] }" 

and data generate map<string, hashset<string>>.

essentially want reverse this procedure.

all code project can found on github page here, it's quite short.


update

        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 = 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 ! // \\ // \\ // \\ // \\ // \\ // \\        (map.entry<string, hashset<string>> entry : map.entryset())         {            system.out.println(entry.getkey()+" : " + arrays.deeptostring(map.entryset().toarray()) );        } 

using 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()); 

update:

use typetoken

type type = new typetoken<map<string, hashset<string>>>(){}.gettype(); map = (map<string, hashset<string>>) gson.fromjson(json, type); 

or parse it...

  • create object of jsonobject
  • create object of hashmap
  • iterate on jsonobj.keys() , every key value jsonobj.getstring(key).
  • put in map map.put(key, value).