Gson is a Java class library provided by Google for mapping between Java objects and JSON data. You can turn a JSON string into a Java object, or vice versa.
Jar and Source: http://code.google.com/p/google-gson/downloads/list
Entity class:
Public classStudent {Private intID; PrivateString name; PrivateDate BirthDay; Public intgetId () {returnID; } Public voidSetId (intID) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicDate Getbirthday () {returnBirthDay; } Public voidSetbirthday (Date birthDay) { This. BirthDay =BirthDay; } @Override PublicString toString () {return "Student [birthday="+ BirthDay +", id="+ ID +", Name="+ name +"]"; }}
Test class:
import java.util.arraylist;import java.util.date;import java.util.list;import Com.google.gson.gson;import Com.google.gson.reflect.TypeToken; Public classGsonTest1 { Public Static voidMain (string[] args) {Gson Gson=NewGson (); Student Student1=NewStudent (); Student1.setid (1); Student1.setname ("kun"); Student1.setbirthday (NewDate ()); // //////////////////////////////////////////////////////////System. out. println ("----------conversions between simple objects-------------"); //simple bean to JSONString S1 =Gson.tojson (STUDENT1); System. out. println ("Simple bean conversion to json==="+S1); //json to Simple beanStudent Student = Gson.fromjson (S1, Student.class); System. out. println ("JSON converted to simple bean==="+student); //Results://simple Bean converted to json==={"id": 1, "name": "Kun", "BirthDay": "June," 8:27:52 AM "}//JSON converted to simple bean===student [Birthday=fri June 08:27:52 CST, id=1,//name= Kun]// //////////////////////////////////////////////////////////Student Student2=NewStudent (); Student2.setid (2); Student2.setname ("Cao Guisheng"); Student2.setbirthday (NewDate ()); Student Student3=NewStudent (); Student3.setid (3); Student3.setname ("Sharon"); Student3.setbirthday (NewDate ()); List<Student> list =NewArraylist<student>(); List.add (STUDENT1); List.add (Student2); List.add (STUDENT3); System. out. println ("----------Conversions between lists with generics-------------"); //list with generics converted to JSONString s2 =Gson.tojson (list); System. out. println ("list with generics converted to json=="+S2); //JSON to a list with genericsList<student> retlist =Gson.fromjson (S2,NewTypetoken<list<student>>() {}.gettype ()); for(Student stu:retlist) {System. out. println (Stu); } //Results://list with generics converted to json==[{"id": 1, "name": "Kun", "BirthDay": "June," "8:28:52 AM"},{"id": 2, "name": "Cao Guisheng", "BirthDay": " June, 8:28:52 AM "},{" id ": 3," "Name": "Sharon", "BirthDay": June, 8:28:52 AM "}]//Student [Birthday=fri June 08:28:52 CST, id=1, Name= Kun]//Student [Birthday=fri June 08:28:52 CST, id=2, name= Cao Guisheng]//Student [Birthday=fri June 08:28:52 CST, id=3, name= Sharon] }}
Execution Result:
----------conversions between simple objects-------------Simple bean conversion to JSON==={"ID":1,"name":"kun","BirthDay":"June, 9:10:31 PM"}json switch to Simple bean===student [Birthday=fri June A +:Ten: toCst -, id=1, name=Kun]----------conversions between lists with generics-------------list with generics converted to JSON==[{"ID":1,"name":"kun","BirthDay":"June, 9:10:31 PM"},{"ID":2,"name":"Cao Guisheng","BirthDay":"June, 9:10:31 PM"},{"ID":3,"name":"Sharon","BirthDay":"June, 9:10:31 PM"}]student [BirthDay=fri June A +:Ten: toCst -, id=1, name=Kun] Student [BirthDay=fri June A +:Ten: toCst -, id=2, name=Cao Guisheng] Student [BirthDay=fri June A +:Ten: toCst -, id=3, Name= Sharon]
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 requirements processing in real development (http://blog.csdn.net/lk_blog/article/details/7685237)
JSON conversion Tool Gson example six-register Typeadapter and process enum type (http://blog.csdn.net/lk_blog/article/details/7685347)
Instance code download: http://download.csdn.net/detail/lk_blog/4387822
Start from scratch learn Android development-json Conversion Tool Gson Example