Android development _ Gson Analysis

Source: Internet
Author: User

// Converter GsonBuilder builder = new GsonBuilder (); // The Field builder without the @ Expose annotation is not converted. excludeFieldsWithoutExposeAnnotation (); Gson gson = builder. create (); // 1. Convert the object to string Student stu = new Student (); stu. setStudentId (333); stu. setStudentName ("qqq"); String stuStr = gson. toJson (stu); System. out. println (stuStr); // {"studentName": "qqq", "studentId": 333} // 2. The string object is 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); // 3. Convert Object List to string List <Student> testBeanList = new ArrayList <Student> (); Student testBean = new Student (); testBean. setStudentId (555); testBean. setStudentName ("552"); testBeanList. add (testBean); // Gson gsonList = new Gson (); Type = new TypeToken <List <Student> (){}. getType (); // specify the property String beanListToJson = gson. toJson (testBeanList, type); System. out. println (beanListToJson); // [{"studentName": "552", "studentId": 555}] // list of conversion objects from a collection string <Student> testBeanListFromJson = gson. fromJson (beanListToJson, type); System. out. println (testBeanListFromJson); // [555: 552] // 4. If the set is not specified, the default type is String List <String> testList = new ArrayList <String> (); testList. add ("first"); testList. add ("second"); String listToJson = gson. toJson (testList); System. out. println (listToJson); // ["first", "second"] // 5. To return the set String, you must specify the type List <String> testList2 = (List <String>) gson. fromJson (listToJson, new TypeToken <List <String> (){}. getType (); System. out. println (testList2); // 6. convert a HashMap String to a JSON 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 "} // 7. stringMap to 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} // 8. objects contain common objects, sets, and maps. Student user1 = new Student (); user1.setStudentId (1001); user1.setStudentName ("James "); student user3 = new Student (); user3.setStudentId (1002); user3.setStudentName (""); 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": "Li Si ", "studentId": 1002}], "student": {"studentName": "Zhang San", "studentId": 1001}, "map": {"user3 ": {"studentName": "Li Si", "studentId": 1002}, "user1": {"studentName": "Zhang San", "studentId": 1001 }}, "id": 0, "age": 0} */Java code // 9. the string of a complex object is converted to the Teacher groupBean2 = (Teacher) gson. fromJson (sGroupBean, new TypeToken <Teacher> (){}. getType (); System. out. println (groupBean2); Java code package com. andtools; import com. google. gson. annotations. expose; public class Student {@ Expose private String studentName; @ 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;} Java code 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 age) {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 ;}}

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.