fastjson--Ali Open Source's fastest JSON and object conversion tool

Source: Internet
Author: User

Example
Importjava.util.ArrayList;Importjava.util.List;ImportJava.util.HashMap;ImportJava.util.Map;ImportCom.alibaba.fastjson.JSON;ImportCom.alibaba.fastjson.JSONArray;ImportCom.alibaba.fastjson.JSONObject;classuser{Private intID; PrivateString name;  Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }}classgroup{Private intID; PrivateString name; Privatelist<user> list =NewArraylist<user>();  Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicList<user>getList () {returnlist; }     Public voidSetlist (list<user>list) {         This. List =list; }}classWeibo {PrivateString ID; PrivateString City;  PublicWeibo (string ID, String city) { This. ID =ID;  This. City =City ; }     PublicWeibo () {} PublicString getId () {returnID; }     Public voidsetId (String id) { This. ID =ID; }     PublicString getcity () {returnCity ; }     Public voidsetcity (String city) { This. City =City ; }} Public classJsondemo { Public Static voidMain (string[] args) {json2jsonobjectm1 ();//converts JSON text data information to a Jsonobject object and then obtains information using key-value pairsJSON2BEANM2 ();//convert JSON text data to JavaBean data! JSON2JSONARRAYM3 ();//Convert JSON data to Jsonarray:JSON2JAVABEANM4 ();//Converts JSON text to a collection of JavaBean, converted to a simple 1 object:parseobject (String text, class<t> clazz);BEAN2JSONM5 ();//convert JavaBean to JSON-formatted data/json the delivery of text user data: For example, uploading a serverBEAN2JSONOBJECTM6 ();//convert JavaBean to JSON objectComplexExampleM7 ();//a relatively complex example:COMPLEX2JSONM8 ();//a complex object-to-JSON demoCOMPLEXMAP2JSONM9 ();//convert the map data to jsonstring demo; This is not a direct break for the Redboy server!     }    /*** Convert JSON text data information to a Jsonobject object and then obtain information using key-value pairs*/    Private Static voidjson2jsonobjectm1 () {//a jsonobject text dataString s = "{\" name\ ": \" liuzhao\ "}"; //convert Jsonobject data to JSONJsonobject Object =Json.parseobject (s); //get a value by using key-value pairsSystem.out.println (Object.get ("name")); /*** Print: * Liuzhao*/    }    /*** Convert JSON text data to JavaBean data! * Note: The name of the key in the JSON text message must be the same as the field name in JavaBean!     The keys do not appear in this javabean as null! */    Private Static voidjson2beanm2 () {String s= "{\" id\ ": \" 0375\ ", \" city\ ": \" Mesa \ "}"; //a simple and convenient way to convert JSON text information to jsonobject objects while converting to JavaBean objects! Weibo Weibo = Json.parseobject (S, Weibo.class);//the Weibo class is defined belowSystem.out.println (Weibo.getid ());        System.out.println (Weibo.getcity ()); //Printed Results 0375//Pingdingshan    }    /*** Convert data of map type to jsonstring*/    Private Static voidComplexmap2jsonm9 () {Group Group=NewGroup (); Group.setid (1); Group.setname ("Group"); User User1=NewUser (); User1.setid (2); User1.setname ("User1"); User User2=NewUser (); User2.setid (3); User2.setname ("User2");        Group.getlist (). Add (User1);        Group.getlist (). Add (User2); Map<integer, object> map =NewHashmap<integer,object>(); Map.put (1, "the most"); Map.put (2, "No.2"); Map.put (3, Group.getlist ()); String jsonstring=json.tojsonstring (map);        System.out.println (jsonstring); /*** Output: {1: "The Most", 2: "No.2", 3:[{"id": 2, "name": "user1"},{"id": 3, "name": "User2"}]}*/    }    /*** from a complex object to JSON demo*/    Private Static voidCOMPLEX2JSONM8 () {Group Group=NewGroup (); Group.setid (1); Group.setname ("Group"); User User1=NewUser (); User1.setid (2); User1.setname ("User1"); User User2=NewUser (); User2.setid (3); User2.setname ("User2");        Group.getlist (). Add (User1);        Group.getlist (). Add (User2); String jsonstring=json.tojsonstring (group);        System.out.println (jsonstring); /*** Output: {"id": 1, "list": [{"id": 2, "name": "user1"},{"id": 3, "name": "User2"}], "name": "Group"}*/    }    /*** A relatively complex example: * JSON text data: * First is jsonobject, converted to Jsonarray; * then convert Jsonarray to JavaBean*/    Private Static voidcomplexExampleM7 () {String s= "{js:[{id:\" 110000\ ", \" city\ ": \" North #001 Beijing \ "},{id:\" 110000\ ", \" city\ ": \" North #002 beijing \ "}" + ", {id:\" 110000\ ", \" CIT                Y\ ": \" North #002 Beijing \ "},{id:\" 110000\ ", \" city\ ": \" North #002 Beijing city \ "}," + "{id:\" 110000\ ", \" city\ ": \" #006北 #005 beijing \ "}," + "{id:\" 110000\ ", \" city\ ": \" North #002 beijing \ "}," + "{id:\" 110000\ ", \" city\ ": \" North #002 Beijing \ "},{id:\" 120000\ ", \ "city\": \ "Day #009 Jinshi \"}]} "; Jsonobject Object=Json.parseobject (s); Object Jsonarray= Object.get ("JS");        System.out.println (Jsonarray); List<Weibo> list = Json.parsearray (jsonarray+ "", Weibo.class);  for(Weibo weibo:list) {System.out.println (weibo.getcity ()); }    }    /*** Convert JavaBean to JSON object*/    Private Static voidbean2jsonobjectm6 () {Weibo Weibo=NewWeibo ("0373", "Luoyang"); Jsonobject JSON=(Jsonobject) Json.tojson (Weibo); System.out.println (Json.get ("id")); /*** Print: * 0373*/    }    /*** Convert JSON text to JavaBean collection; * Internal implementations must be converted to Jsonarray first and then to the list collection*/    Private Static voidjson2javabeanm4 () {String s= "[{\" id\ ": \" 0375\ ", \" city\ ": \" Mesa \ "},{\" id\ ": \" 0377\ ", \" city\ ": \" Nanyang \ "}]"; List<Weibo> list = Json.parsearray (S, Weibo.class);  for(Weibo weibo:list) {System.out.println (weibo.getcity ()); }        /*** Print: * Pingdingshan * Nanyang*/    }    /*** Convert JSON data to Jsonarray: * Note: After getting to Jsonarray (we just get to Jsonarray instead of the list collection of JavaBean) * Get the data in Jsonarray convert to Stri     The NG type needs to be added "" outside; */    Private Static voidjson2jsonarraym3 () {String s= "[{\" id\ ": \" 0375\ ", \" city\ ": \" Mesa \ "},{\" id\ ": \" 0377\ ", \" city\ ": \" Nanyang \ "}]"; //convert JSON text to JsonarrayJsonarray array =Json.parsearray (s); //This line must be written: must be added + "", otherwise the type of strong turn exception will be reported! String str = array.get (1) + ""; System.out.println (Array.get (0)); Jsonobject Object=json.parseobject (str); System.out.println (Object.get ("id")); /*** {"City": "Mesa", "id": "0375"} 0377*/    }    /*** Convert JavaBean to JSON-formatted data/json text * User data transfer: For example, uploading a server*/    Private Static voidbean2jsonm5 () {Weibo Weibo=NewWeibo ("123456", "Shanghai"); String String=json.tojsonstring (Weibo);        System.out.println (string); /*** Print: * {"City": "Shanghai", "id": "123456"}*/    }}
Reference

http://www.oschina.net/code/snippet_228315_35122

fastjson--Ali Open Source's fastest JSON and object conversion tool

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.