JSON, Map Mutual transfer

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.