How do i refactor one JSON array into other format in JAVA? -



How do i refactor one JSON array into other format in JAVA? -

i have json array this,

jsonarray content= "[ {"+"\"pageid\":\"19\","+"\"company\":"+"\"c1\","+"\"pageview\":"+"\"10\","+"\"visitfreq\":"+"\"2\"},{"+"\"pageid\":\"19\","+"\"company\":"+"\"c2\","+"\"pageview\":"+"\"20\","+"\"visitfreq\":"+"\"4\"},{"+"\"pageid\":\"200\","+"\"company\":"+"\"c3\","+"\"pageview\":"+"\"30\","+"\"visitfreq\":"+"\"3\"} ]";

code jsonarray:

jsonobject jobj1 = new jsonobject(); jobj1.put("pageid", "19"); jobj1.put("company", "c1"); jobj1.put("pageview", "10"); jobj1.put("visitfreq", "2"); jsonobject jobj2 = new jsonobject(); jobj2.put("pageid", "19"); jobj2.put("company", "c2"); jobj2.put("pageview", "20"); jobj2.put("visitfreq", "4"); jsonobject jobj3 = new jsonobject(); jobj3.put("pageid", "200"); jobj3.put("company", "c3"); jobj3.put("pageview", "30"); jobj3.put("visitfreq", "3"); jsonarray jarr = new jsonarray(); jarr.put(jobj1); jarr.put(jobj2); jarr.put(jobj3);

visual representation:

[ { "company": "c1", "visitfreq": "2", "pageview": "10", "pageid": "19" }, { "company": "c2", "visitfreq": "4", "pageview": "20", "pageid": "19" }, { "company": "c3", "visitfreq": "3", "pageview": "30", "pageid": "200" } ]

i have worked on out set below

the out set this

[{pageid},[{rest of details}] ,{pageid},[{rest of details}] ]

if same pageid occur more once should this

[{pageid},[{rest of details1},{rest of details2},.. ],{pageid},[{rest of details}] ]

looks need create map maps pageid : [{rest of details1},{rest of details2},.. ].

once have map, can transform desired output. here code take jsonarray (the 1 in code above) , turn map<string, jsonarray>.

public map<string, jsonarray> getjsonarrayasmaponpageid(jsonarray inputarr) throws jsonexception { //map holding pageid : [{company:"",pageview:"",visitfreq:""}, ... , ...] mappings map<string, jsonarray> idmap = new hashmap<string, jsonarray>(); (int i=0; < inputarr.length(); i++) { jsonobject inputobj = inputarr.getjsonobject(i); string id = inputobj.getstring("pageid"); jsonarray jobjlist; if (idmap.containskey(id)) { //append existing list in map jobjlist = idmap.get(id); } else { //create new list jobjlist = new jsonarray(); } jsonobject newjsonobj = new jsonobject(); newjsonobj.put("company", inputobj.get("company")); newjsonobj.put("pageview", inputobj.get("pageview")); newjsonobj.put("visitfreq", inputobj.get("visitfreq")); jobjlist.put(newjsonobj); idmap.put(id, jobjlist); } homecoming idmap; }

here it'll like:

system.out.println("map looks like:\n" + pageidmap);

here got:

{ 200=[ { "company": "c3", "visitfreq": "3", "pageview": "30" } ], 19=[ { "company": "c1", "visitfreq": "2", "pageview": "10" }, { "company": "c2", "visitfreq": "4", "pageview": "20" } ] }

i'm not sure in form (object, print output, etc) want output in, can final bit of transforming map<string, jsonarray> [{pageid},[{rest of details1},{rest of details2},.. ],{pageid},[{rest of details}] ] without problems.

java json arrays

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -