Gson is an open source project for Google that can convert Java objects to JSON or convert JSON to Java objects.
The most important objects in Gson are 2 Gson and Gsonbuilder.
Gson has 2 most basic methods.
1) ToJson () – Convert Java objects to JSON
2) Fromjson () – Convert JSON to Java object
For generic objects, use the Fromjson (String, Type) method to convert the JSON object to the corresponding generic object
The new typetoken<> () {}.gettype () method gets the type
such as: New Typetoken<list<string>> () {}.gettype ()
New Typetoken<list<person>> () {}.gettype ()
New typetoken<list<map<string, string>>> () {}.gettype ()
Gsonbuilder
Note that the Gson is constructed in Gsonbuilder, which differs from gson Gson = new Gson ();
Gson Gson = new Gsonbuilder ()
. Excludefieldswithoutexposeannotation ()//Do not export attributes with @expose annotations in the entity
. Enablecomplexmapkeyserialization ()//support map key as complex object form
. Serializenulls (). Setdateformat ("Yyyy-mm-dd HH:mm:ss:SSS")//Time conversion to a specific format
. Setfieldnamingpolicy (Fieldnamingpolicy.upper_camel_case)//will capitalize the first letter of the field, note: The @serializedname annotation used on the entity will not take effect.
. setprettyprinting ()//format JSON results.
. Setversion (1.0)//Some of the fields are not initially available and will be added as the version is upgraded, so the serialization and serialization are selected for serialization based on the version number.
The @Since (version number) is perfect for this function. Also the fields may be deleted as the version is upgraded, then
@Until (version number) can also implement this function, the gsonbuilder.setversion (double) method needs to be called.
. Create ();
Reference reading
JSON conversion Tool Gson example one-Simple object conversion and list conversion with generics http://blog.csdn.net/lk_blog/article/details/7685169
JSON conversion Tool Gson Example two-gson annotations and Gsonbuilder http://blog.csdn.net/lk_blog/article/details/7685190
JSON conversion Tool Gson example of three-map processing (upper) http://blog.csdn.net/lk_blog/article/details/7685210
JSON conversion Tool Gson example four-map processing (bottom) http://blog.csdn.net/lk_blog/article/details/7685224
JSON conversion Tool Gson example five-special needs handling in real development http://blog.csdn.net/lk_blog/article/details/7685237 important
JSON conversion Tool Gson example six-register Typeadapter and deal with enum type http://blog.csdn.net/lk_blog/article/details/7685347 infrequently used
Code
Auxiliary classes
public class Jsonservice
{
Public Person Getperson ()
{
person person = new person (1, "Xiaoluo", "Guangzhou");
return person;
}
Public list<person> getpersons ()
{
list<person> persons = new arraylist<person> ();
person person = new person (1, "Xiaoluo", "Guangzhou");
Person Person2 = new Person (2, "Android", "Shanghai");
Persons.add (person);
Persons.add (Person2);
return persons;
}
Public list<string> getString ()
{
list<string> list = new arraylist<string> ();
List.add ("Guangzhou");
List.add ("Shanghai");
List.add ("Beijing");
return list;
}
Public list<map<string, string>> getmaplist ()
{
list<map<string, string>> list = new arraylist<map<string, string>> ();
map<string, string> map1 = new hashmap<string, string> ();
Map1.put ("id", "001");
Map1.put ("name", "Xiaoluo");
Map1.put ("Age", "20");
map<string, string> map2 = new hashmap<string, string> ();
Map2.put ("id", "002");
Map2.put ("name", "Android");
Map2.put ("Age", "33");
List.add (MAP1);
List.add (MAP2);
return list;
}
}
Test class
public static void Main (string[] args)
{
Gson Gson = new Gson ();
Jsonservice jsonservice = new Jsonservice ();
Person person = Jsonservice.getperson ();
System.out.println ("Person:" + gson.tojson (person));
For object type, use the Fromjson (String, Class) method to convert the JSON object to a Java object
Person Person2 = Gson.fromjson (Gson.tojson (person), person.class);
System.out.println (Person2);
System.out.println ("------------------------------------------------");
list<person> persons = Jsonservice.getpersons ();
System.out.println ("Persons:" + Gson.tojson (persons));
/*
* For generic objects, use the Fromjson (String, Type) method to convert the JSON object to the corresponding generic object
* New Typetoken<> () {}.gettype () method
*/
list<person> persons2 = Gson.fromjson (Gson.tojson (persons), new Typetoken<list<person>> () {}. GetType ());
System.out.println (PERSONS2);
System.out.println ("------------------------------------------------");
list<string> list = jsonservice.getstring ();
System.out.println ("String---->" + gson.tojson (list));
List<string> List2 = Gson.fromjson (Gson.tojson (list), new typetoken<list<string>> () {}.getType ());
System.out.println ("list2---->" + list2);
System.out.println ("------------------------------------------------");
list<map<string, string>> listmap = Jsonservice.getmaplist ();
System.out.println ("Map---->" + Gson.tojson (listmap));
list<map<string, string>> listMap2 = Gson.fromjson (Gson.tojson (Listmap), new Typetoken<list<map <string, string>>> () {}.gettype ());
System.out.println ("listMap2---->" + LISTMAP2);
System.out.println ("------------------------------------------------");
}
Output results
Person: {"id": 1, "name": "Xiaoluo", "Address": "Guangzhou"}
person [id=1, Name=xiaoluo, address= Guangzhou]
------------------------------------------------
Persons: [{"id": 1, "name": "Xiaoluo", "Address": "Guangzhou"},{"id": 2, "name": "Android", "Address": "Shanghai"}]
[Person [Id=1, Name=xiaoluo, address= Guangzhou], person [id=2, name=android, address= Shanghai]
------------------------------------------------
String---->["Guangzhou", "Shanghai", "Beijing"
List2---->[Guangzhou, Shanghai, Beijing]
------------------------------------------------
MAP---->[{"id": "001", "Age": "A", "name": "Xiaoluo"},{"id": "002", "Age": "$", "name": "Android"}]
LISTMAP2---->[{id=001, age=20, Name=xiaoluo}, {id=002, age=33, name=android}]
------------------------------------------------
The Gson of JSON parsing