Java Study Notes: In-depth use of gson

Source: Internet
Author: User

Gson and other existing java json class libraries are the largest and different. gson needs to serialize the real class and does not need to use annotation to identify the fields to be serialized, at the same time, gson can use annotation to flexibly configure fields to be serialized. Object To JSON: it is very easy to convert an object to JSON. You only need to put the object into the parameters of the toJson function. You can [java] HashMap <String, String> map = new HashMap <String, String> (); // instantiate a hashmap map. put ("username", "initphp"); // Add a Data map. put ("password", "test"); map. put ("age", "100"); String mapToJson = gson. toJson (map); returned result: [html] {"username": "initphp", "age": "100", "age2": "age2", "age1 ": "age1", "password": "test"} converting a JSON string to an object: converting a JSON string to an object is a little complicated. It can be divided into multiple situations. First, we cannot determine the data structure in JSON, but we can determine that this JSON is a one-dimensional structure. We can use HashMap to load this data. [Java] String hashString = "{\" username \ ": \" initphp \ ", \" age \ ": \" 100 \ "}"; HashMap <String, string> testBeanListFromJson = gson. fromJson (hashString, new TypeToken <HashMap <String, String >> (){}. getType (); testBeanListFromJson. get ("age") second type, with a definite data structure: there is a person class [java] publicclass Person {private String name; privateint age; /*** @ return the name */public String getName () {return name;}/*** @ param name the name to set */publicvoid setName (String name) {this. name = name;}/*** @ return the age */publicint getAge () {return age ;} /*** @ param age the age to set */publicvoid setAge (int age) {this. age = age ;}@ Override public String toString () {return name + ":" + age ;}} [java] Gson gson = new Gson (); list <Person> persons = new ArrayList <Person> (); for (int I = 0; I <10; I ++) {Person p = new Person (); p. setName ("name" + I); p. setAge (I * 5); persons. add (p);} String str = gson. result: [java] www.2cto.com [{"name": "name0", "age": 0 },{ "name": "name1 ", "age": 5 },{ "name": "name2", "age": 10 },{ "name": "name3", "age": 15 }, {"name": "name4", "age": 20 },{ "name": "name5", "age": 25 },{ "name ": "name6", "age": 30 },{ "name": "name7", "age": 35 },{ "name": "name8", "age ": 40 },{ "name": "name9", "age": 45}] single data, which can be [java] Person person = gson. fromJson (str, Person. class );

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.