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 );