Sonobjetc and string Map beans, Jsonarray and string list mutual transfer

Source: Internet
Author: User

1 Importjava.util.ArrayList; 2 ImportJava.util.HashMap; 3 Importjava.util.List; 4 ImportJava.util.Map; 5   6 ImportCom.alibaba.fastjson.JSONArray; 7 ImportCom.alibaba.fastjson.JSONObject; 8   9   Ten  Public classJsonparse { One    A      Public Static voidMain (string[] args) { - //String str= "{' UID ': ten, ' uname ': ' Zhang San '}";  - //System.out.println (Stringtojsonobject (str));//String Turn Jsonobject the            - //hashmap<string, object> map = new hashmap<string, object> ();  - //map.put ("UID", 11);  - //map.put ("uname", "Zhang San");  + //System.out.println (Maptojsonobject (map));//Map Turn Jsonobject -            + //User User=new User ();  A //User.setuid (12);  at //user.setuname ("Zhang San");  - //System.out.println (beantojsonobject (user));//Bean Turn Jsonobject -            - //jsonobject jo=stringtojsonobject (str);  - //System.out.println (jsonobjecttostring (Jo));//Jsonobject Turn String -            in //map<string, object> map=jsonobjecttomap (Jo);  - //System.out.println (map); //Jsonobject Turn map<stirng,object> to        + //User U=jsonobjecttobean (JO);  - //System.out.println (u);//Jsonobject Turn Bean the            *            $list<map<string, object>> list =NewArraylist<map<string, object>>(); Panax Notoginsengmap<string, object> map =NewHashmap<string, object>();  -map<string, object> map1 =NewHashmap<string, object>();  theMap.put ("UID", 10);  +Map.put ("uname", "Zhang San");  AMap1.put ("UID", 11);  theMap1.put ("uname", "John Doe");  + list.add (map);  - List.add (MAP1);  $ //System.out.println (Listtojsonarray (list));//List Turn Jsonarray $            - //String Str=listtojsonarray (list). toJSONString ();  - //System.out.println (Stringtojsonarray (str));//string Turn Jsonarray the            -Jsonarray ja=Listtojsonarray (list); Wuyi //System.out.println (jsonarraytostring (JA));  the System.out.println (jsonarraytolist (JA));  -            Wu     }   -        About     /**  $ * String Turn jsonobject -      * @paramstring -      * @returnJsonobject -      */   A      Public Staticjsonobject stringtojsonobject (String str) { +         returnjsonobject.parseobject (str);  the     }   -        $     /**  the * Map Turn Jsonobject the      * @paramstring the      * @returnJsonobject the      */   -      Public StaticJsonobject Maptojsonobject (map<string, object>map) {   inJsonobject jo=NewJsonobject ();  the Jo.putall (map);  the         returnJo;  About     }   the        the     /**  the * Bean Turn jsonobject +      * @paramstring -      * @returnJsonobject the      */  Bayi      Public Staticjsonobject beantojsonobject (user user) { the         return(jsonobject) jsonobject.tojson (user);  the     }   -        -     /**  the * Jsonobject to String the      * @paramJsonobject the      * @returnstring the      */   -      Public StaticString jsonobjecttostring (jsonobject Jo) { the         returnjo.tojsonstring ();  the     }   the       94     /**  the * Jsonobject Transfer map the      * @paramJsonobject the      * @returnstring98      */   About      Public StaticMap<string, object>Jsonobjecttomap (jsonobject Jo) { -         returnJsonobject.tojavaobject (Jo, Map.class); 101     }  102       103     /** 104 * Jsonobject Turn Bean the      * @paramJsonobject106      * @returnstring107      */  108      Public StaticUser Jsonobjecttobean (jsonobject Jo) {109         returnJsonobject.tojavaobject (Jo, User.)class);  the     }  111        the       113     /**  the * String Turn Jsonarray the      * @paramString the      * @returnJsonarray117      */  118      Public Staticjsonarray Stringtojsonarray (String str) {119         returnJsonarray.parsearray (str);  -     }  121       122       123     /** 124 * List Turn Jsonarray the      * @paramList126      * @returnJsonarray127      */   -      Public StaticJsonarray Listtojsonarray (list<map<string, object>>list) {  129Jsonarray ja=NewJsonarray ();  the Ja.addall (list); 131         returnja;  the     }  133       134     /** 135 * Jsonarray Turn String136      * @paramJsonarray137      * @returnString138      */  139      Public StaticString jsonarraytostring (Jsonarray ja) { $         returnja.tojsonstring (); 141     }  142       143     /** 144 * Jsonarray Turn List145      * @paramJsonarray146      * @returnlist<map<string, object>>147      */  148      Public StaticList<map<string, object>>jsonarraytolist (Jsonarray ja) {149         returnJsonarray.tojavaobject (JA, List.class);  Max     }  151}

The JSON used is: Fastjson-1.2.3.jar

Reprinted from: http://blog.csdn.net/liangwenmail/article/details/52432443

Sonobjetc and string Map beans, Jsonarray and string list 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.