Convert JSON format using Fastjson

Source: Internet
Author: User

1. Download the jar Package

http://repo1.maven.org/maven2/com/alibaba/fastjson/

2. Importing a jar package into a project 3. Example
 PackageNc.testfastjson;Importjava.util.ArrayList;ImportJava.util.HashMap;Importjava.util.LinkedList;Importjava.util.List;ImportJava.util.Map;ImportJava.util.Map.Entry;ImportJava.util.Set;ImportCom.alibaba.fastjson.JSON;ImportCom.alibaba.fastjson.JSONObject;Importcom.alibaba.fastjson.TypeReference; Public classTestfastjson { Public Static voidMain (string[] args) { //  Java object to JSON People p1 =NewPeople ("People_1", "Male", 1); String P1_json=json.tojsonstring (p1);                System.out.println (P1_json.tostring ());  //  JSON to Java object String P2_json = "{' name ': ' people_2 ', ' sex ': ' Male ', ' Age ': 2}"; People P2= Json.parseobject (P2_json, people.class);                System.out.println (P2.tostring ());  //  Java Object LinkedList Collection goto JSON Linkedlist<people> p_list =NewLinkedlist<>(); People P3=NewPeople ("People_3", "Male", 3); People P4=NewPeople ("People_4", "Male", 4); People P5=NewPeople ("People_5", "Male", 5);        P_list.add (p3);        P_list.add (p4);                P_list.add (p5); String P_list_json=json.tojsonstring (p_list);                System.out.println (P_list_json);  //  JSON to Java Object List collection list<people> p_list_2 = Json.parsearray (P_list_json, people.class);  for(People people:p_list_2) {System.out.println (people.tostring ()); }                 //  Java object ArrayList to JSON Arraylist<people> ArrayList =NewArraylist<>();        Arraylist.add (p3);        Arraylist.add (p4);                Arraylist.add (p5); String Arrays_json=json.tojsonstring (arrayList);                System.out.println (Arrays_json); // JSON to Java Object List collection list<people> arrayList2 = Json.parsearray (Arrays_json, people.class);  for(People people:arraylist2) {System.out.println (people.tostring ()); }                 //  map to JSON HashMap<string,people> map =NewHashmap<>(); Map.put ("P3", p3); Map.put ("P4", p4); Map.put ("P5", p5); String Map_json=json.tojsonstring (map);                System.out.println (Map_json);  //  JSON to map Map<string, string> map2 = Jsonobject.parseobject (map_json.tostring (),NewTypereference<map<string, string>>(){}); Set<entry<string, string>> entryset =Map2.entryset ();  for(Entry<string, string>Entry:entryset) {String key=Entry.getkey (); String value=Entry.getvalue (); People p= Json.parseobject (value, people.class); SYSTEM.OUT.PRINTLN (Key+":"+p.tostring ()); }            }}
 PackageNc.testfastjson; Public classpeople {PrivateString name; PrivateString sex; Private intAge ;  PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicString Getsex () {returnsex; }     Public voidsetsex (String sex) { This. Sex =sex; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; } @Override PublicString toString () {return"People [name=" + name + ", sex=" + Sex + ", age=" + Age + "]"; }         Publicpeople () {Super(); }     PublicPeople (string name, String sex,intAge ) {        Super();  This. Name =name;  This. Sex =sex;  This. Age =Age ; }    }

Convert JSON format using Fastjson

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.