With Gson, we can easily transform the data objects and JSON objects, where we use two methods, one is Fromjson (), we convert the JSON object to the data object we need, and the other is Tojson (), This is the conversion of our data objects into JSON objects. Let's look at a comprehensive example of how Gson is used:
Gson (http://code.google.com/p/google-gson/)
Public classJsonservice { PublicPerson Getperson () { person person=NewPerson (1,"Xiaoluo","Guangzhou"); returnPerson ; } PublicList<person>getpersons () {List<Person> persons =NewArraylist<person>(); Person Person=NewPerson (1,"Xiaoluo","Guangzhou"); Person Person2=NewPerson (2,"Android","Shanghai"); Persons.add (person); Persons.add (Person2); returnpersons; } PublicList<string>getString () {List<String> list =NewArraylist<string>(); List.add ("Guangzhou"); List.add ("Shanghai"); List.add ("Beijing"); returnlist; } PublicList<map<string, string>>getmaplist () {List<map<string, string>> list =NewArraylist<map<string, string>>(); Map<string, string> map1 =NewHashmap<string, string>(); Map1.put ("ID","001"); Map1.put ("name","Xiaoluo"); Map1.put (" Age"," -"); Map<string, string> map2 =NewHashmap<string, string>(); Map2.put ("ID","002"); Map2.put ("name","Android"); Map2.put (" Age"," -"); List.add (MAP1); List.add (MAP2); returnlist; }}
Public classPerson {Private intID; PrivateString name; PrivateString address; PublicPerson () {} Public intgetId () {returnID; } Public voidSetId (intID) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicString getaddress () {returnaddress; } Public voidsetaddress (String address) { This. Address =address; } PublicPerson (intID, string name, string address) {super (); This. ID =ID; This. Name =name; This. Address =address; } @Override PublicString toString () {return "Person [id="+ ID +", Name="+ name +", address="+Address+"]"; }}
Public classTest { Public voidname () {} Public Static voidMain (string[] args) {//Person person = new person (1, "Xiaoluo", "Guangzhou"); //converts a person object to a JSON-type string Object//String personstring = jsontools.getjsonstring ("person", person); //System.out.println (personstring.tostring ());Gson Gson =NewGson (); Jsonservice Jsonservice=NewJsonservice (); 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 objectPerson 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),NewTypetoken<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),NewTypetoken<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),NewTypetoken<list<map<string, string>>>() {}.gettype ()); System. out. println ("LISTMAP2---->"+listMap2); System. out. println ("------------------------------------------------"); }}
JSON of Java