Transparent gson@expose annotations, @SerializedName, parsing JSON data

Source: Internet
Author: User
Tags string to json tojson

Before telling how to parse the data, describe the two annotations @expose and @serializedname in Gson.

@Expose annotations: Distinguishes between attributes in an entity that do not want to be serialized, which itself contains two properties deserialize (deserialization) and serialize (serialization), which by default is true.

Use new Gsonbuilder (). Excludefieldswithoutexposeannotation (). Create (); Creating Gson objects, properties without @expose annotations will not be serialized.

Private class user{      private int id;      @Expose      private String name;      .......}
This allows the Create () Gson object to serialize the user with only the name attribute

@SerializedName Annotations: Define the name of the property after serialization

public class user{     private int id;     @Expose     @SerializedName ("username")     private String name;     .......}

Alternatively, you can use transient if you want to not serialize a property.

Private class user{      private int id;      private transient String name;      .......}

The following is a list of how Gson parses JSON data

Converter Gsonbuilder builder = new Gsonbuilder (); <span style= "color: #ff0000;" >//does not convert fields with no @Expose annotations </span>builder.excludefieldswithoutexposeannotation (); Gson Gson = Builder.create (); <span style= "color: #ff0000;" &GT;//1, object to String</span>student stu = new Student (); Stu.setstudentid (333); Stu.setstudentname ("QQQ"); String stustr = Gson.tojson (stu); System.out.println (STUSTR); {"Studentname": "QQQ", "StudentID": 333}<span style= "color: #ff0000;" &GT;//2, string to object </span>student user2 = Gson.fromjson (Stustr, Student.class); System.out.println (User2); String stutemp = "{\" studentname\ ": \" qqq2\ ", \" studentid\ ": 3335}"; Student user4 = Gson.fromjson (stutemp, Student.class); System.out.println (user4); <span style= "color: #ff0000;" &GT;//3, Object List to string</span>list<student> testbeanlist = new arraylist<student> (); Student Testbean = new Student () Testbean.setstudentid (555); Testbean.setstudentname ("552"); Testbeanlist.add ( Testbean);//gson gsonlist = new Gson (); Type type = new Typetoken<list<student>> () {}.gettype (); Specifies the collection object property of String Beanlisttojson = Gson.tojson (testbeanlist, type); System.out.println (Beanlisttojson); [{"Studentname": "552", "StudentID": 555}]<span style= "color: #ff0000;" >//collection string to object list</span>list<student> Testbeanlistfromjson = Gson.fromjson (BeanListToJson, type); System.out.println (Testbeanlistfromjson); [555:552]<span style= "color: #ff0000;" &GT;//4, sets if the type is not specified by default to string</span>list<string> testlist = new arraylist<string> (); TestList.add (" First "); Testlist.add (" second "); String Listtojson = Gson.tojson (testlist); System.out.println (Listtojson); ["First", "second"]<span style= "color: #ff0000;" &GT;//5, the collection string is returned to the specified type </span>List<String> testList2 = (list<string>) Gson.fromjson (Listtojson, New Typetoken<list<string>> () {}.gettype ()); System.out.println (TESTLIST2); <span style= "color: #ff0000;" &GT;//6, converts the HashMap string to json</span>map<string, string> testMap = new hashmap<string, string> (), Testmap.put ("id", "Id.first"), Testmap.put ("name", "Name.second"); String Maptojson = Gson.tojson (TestMap); System.out.println (Maptojson); {"id": "Id.first", "name": "Name.second"}<span style= "color: #ff0000;" &GT;//7, Stringmap to Object </span>map<string, string> userMap2 = (map<string, string>) Gson.fromJson ( Maptojson,new typetoken<map<string, string>> () {}.gettype ()); System.out.println (USERMAP2); {Id=id.first, Name=name.second}<span style= "color: #ff0000;" &GT;//8, objects containing ordinary objects, sets, map conditions </span>student user1 = new Student (); User1.setstudentid (1001); User1.setstudentname ( "Zhang San"); Student User3 = new Student () User3.setstudentid (1002); User3.setstudentname ("John Doe"); map<string, student> usermap = new hashmap<string, student> (); Usermap.put ("user1", User1); Usermap.put (" User3 ", User3); list<student> userlist = new arraylist<student> (); Userlist.add (user1); Userlist.add (User3); Teacher Groupbean = new Teacher (); GrouPbean.setstudent (user1); Groupbean.setstus (userlist); Groupbean.setmap ((HASHMAP) usermap);//groupbean.setuserlist (userlist); Gson Gsongroup = new Gson (); String Sgroupbean = Gsongroup.tojson (Groupbean, New typetoken<teacher> () {}.gettype ()); System.out.println (Sgroupbean);/*{"Stus": [{"Studentname": "Zhang San", "StudentID": 1001},{"Studentname": "John Doe", "StudentID ": 1002}]," student ": {" Studentname ":" Zhang San "," StudentID ": 1001}," map ": {" User3 ": {" Studentname ":" John Doe "," StudentID ": 1002}, "User1": {"Studentname": "Zhang San", "StudentID": 1001}, "id": 0, "age": 0}*/<span style= "color: #ff0000;" &GT;//9, complex object string to object </span>teacher groupBean2 = (Teacher) Gson.fromjson (sgroupbean,new typetoken<teacher > () {}.gettype ()); System.out.println (GROUPBEAN2); Package Com.andtools;import Com.google.gson.annotations.expose;public class Student {@Expose private String Studentnam  E  @Expose private int StudentID;    Public Student () {} public Student (int studentid,string studentname) {This.setstudentid (StudentID); This.setstudEntname (Studentname);  } public String toString () {return This.getstudentid () + ":" + this.getstudentname ();  } public String Getstudentname () {return studentname;  } public void Setstudentname (String studentname) {this.studentname = Studentname;  } public int Getstudentid () {return studentid;  } public void Setstudentid (int studentid) {this.studentid = StudentID; }} Package Com.andtools;import Java.util.hashmap;import java.util.list;import java.util.map;import  Com.google.gson.annotations.expose;public class Teacher {@Expose private int id;  @Expose private String name;  @Expose private int age;  @Expose private Student Student;  @Expose private List Stus;  @Expose private HashMap map; Public String ToString () {return This.getid () + ":" +this.getname () + ":" +this.getage () + ":" + this.getstudent (). ToString (  ) + ":" + this.getstus () + ":" + this.getmap ();  } public int getId () {return id;  } public void setId (int id) {this.id = ID; } Public String GEtname () {return name;  } public void SetName (String name) {this.name = name;  } public int Getage () {return age;  public void Setage (int.) {this.age = age;  } public Student Getstudent () {return Student;  } public void Setstudent (Student Student) {this.student = Student;  } public List Getstus () {return stus;  } public void Setstus (List stus) {this.stus = Stus;  } public HashMap Getmap () {return map;  } public void Setmap (HashMap map) {this.map = map; }}






Transparent [email protected] annotate, @SerializedName, parse JSON data

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.