First, JSON to map
Method One:
Gson Gson = new Gsonbuilder (). Excludefieldswithoutexposeannotation (). Create (); or Gson Gson = new Gson (); String Strjson For example: Strjson = {"ret": 0, "msg": "", "nickname": "xxx", "Figureurl": "http://qzapp.qlogo.cn/qzapp/100226195/ C399c7b2880641627ced3eef9deb8e30/30 "," Figureurl_1 ":" http://qzapp.qlogo.cn/qzapp/100226195/ C399C7B2880641627CED3EEF9DEB8E30/50 "," figureurl_2 ":" http://qzapp.qlogo.cn/qzapp/100226195/ C399c7b2880641627ced3eef9deb8e30/100 "," Gender ":" XXX "," VIP ":" 0 "," Level ":" 0 "," IS_YELLOW_YEAR_VIP ":" 0 "} Map infomap = Gson.fromjson (Strjson,New typetoken<map<string, string>> () {}.gettype ());Method Two:
Jsonobject jsonobject = new Jsonobject (s);
map<string,object> map= new hashmap<string,object> ();
Iterator it = Jsonobject.keys ();
Traverse jsonobject data, add to map object
while (It.hasnext ())
{
String key = String.valueof (It.next ());
String value = (string) jsonobject.get (key);
Map.put (key, value);
}
return map;
Method Three:
Jsonobject jasonobject = Jsonobject.fromobject (str);
map<string, object> map2 = (Map) jasonobject;
But this method may not be applicable, Baidu, has been a problem, make a record.
Map to JSON
Jsonobject jsonobject = jsonobject.frommap (map);
For Jsonarray, leave an example:
PublicCardataresultlist<carintroentity>Parserjson (String paramstring) throws Car273exception {Try{jsonobject jsonobj=NewJsonobject (paramstring); if(Jsonobj.has (" Total") ) {Defcarintrolist.total= Jsonobj.getint (" Total"); } if(Jsonobj.has ("Info") {Jsonarray Infoarray= Jsonobj.getjsonarray ("Info"); for(inti =0; I < infoarray.length (); i++) {carintroentity carintroentity=Newcarintroentity (); Jsonobject JSON=Infoarray.getjsonobject (i); if(Json.has ("ID") {Carintroentity.setid (json.getstring ("ID")); }
.
.
.
DefCarIntroList.infoList.add (carintroentity); } }} Catch(jsonexception e) {ThrowCE; }
JSON, Map Mutual transfer