Using Gson, we can easily convert a custom Java object to Gson. However, we also need to know some of the transformations between Java basic objects and Gson, and here we say some basic Java objects and Gson direct conversions.
1. Building Jsonobject Objects
In the JSON string, if you want to show the format as {}, you must build the Jsonobject object
eg
Jsonobject spanobj=new jsonobject ();
Spanobj.addproperty ("Latitude_delta", 0);
Spanobj.addproperty ("Longitude_delta", 0);
Jsonobject regionobj=new jsonobject ();
Regionobj.add ("center", centerobj);
Regionobj.add ("span", spanobj);
Jsonobject has 2 add methods, one is AddProperty, the first parameter of this method is key, the second argument is Value,key can only be String type, value can be boolean,integer,string Such as Java basic types
The second method is that the Add,add method also has 2 parameters, respectively, as Key,value, where Value is the Jsonelement object, where jsonelement some subclasses, such as: Jsonarray, Jsonnull, Jsonobject, Jsonprimitive These are all jsonelement subclasses.
2, the construction of Jsonarray, jsonelement objects
In the JSON string, if you want to show that it is in this format, you must build a Jsonarray object or a Jsonelement object
The Jsonarray Add method can only accept jsonelement or its subclasses, so we need to first build a jsonelement or Jsonobject
eg
Jsonarray businessesarray=new Jsonarray ();
Businessesarray.add (businessobj);//businessobj as a Jsonobject object
Since Jsonelement does not have an explicit construction method, if we want to build a jsonarray type of jsonelement, we need to borrow the Gson Tojsontree method, which accepts an object, We can use any Java object to build Jsonelement:
Gson gson=new Gson ();
List<string> cate1=new arraylist<string> ();
Cate1.add ("local Flavor");
Cate1.add ("Localflavor");
List<string> cate2=new arraylist<string> ();
Cate2.add ("Mass Media");
Cate2.add ("Massmedia");
List<list<string>> cates=new arraylist<list<string>> ();
Cates.add (cate1); Cates.add (CATE2);
Jsonelement Categorieselement=gson.tojsontree (Cates);
3. Generate JSON string
If you need to output a JSON string, the first method is to build a Jsonobject object and then output the Jsonobject object, and the second is to generate a string of JSON strings using the Gson Tojson method
Eg: (where obj is a Jsonobject object)
String Gsonstr=gson.tojson (obj);
System.out.println (GSONSTR);
System.out.println (obj);
4, example
Import java.util.ArrayList;
Import java.util.List;
Import Com.google.gson.Gson;
Import Com.google.gson.GsonBuilder;
Import Com.google.gson.JsonArray;
Import com.google.gson.JsonElement;
Import Com.google.gson.JsonObject;
public class Gsonassembled {public static void Assemblegson () {jsonobject obj=new jsonobject ();
Gson gson=new Gson ();
Jsonobject businessobj=new jsonobject ();
List cate1=new ArrayList ();
Cate1.add ("local Flavor");
Cate1.add ("Localflavor");
List cate2=new ArrayList ();
Cate2.add ("Mass Media");
Cate2.add ("Massmedia");
List> cates=new arraylist> ();
Cates.add (cate1); Cates.add (CATE2);
Jsonelement Categorieselement=gson.tojsontree (Cates);
Businessobj.add ("categories", categorieselement);
Businessobj.addproperty ("is_claimed", true);
Businessobj.addproperty ("is_closed", false);
Businessobj.addproperty ("name", "Yelp");
Businessobj.addproperty ("Phone", "4159083801"); Businessobj.addproperty ("menu_date_updated", 1317414369);
Jsonobject userbaseobj1=new jsonobject ();
Userbaseobj1.addproperty ("name", "Yuan");
Userbaseobj1.addproperty ("Sex", "male");
Jsonobject userbaseobj2=new jsonobject ();
Userbaseobj2.addproperty ("name", "Wang");
Userbaseobj2.addproperty ("Sex", "female");
Jsonarray userbasearray=new Jsonarray ();
Userbasearray.add (USERBASEOBJ1);
Userbasearray.add (USERBASEOBJ2);
Jsonobject userbaseobj=new jsonobject ();
Userbaseobj.add ("User_base", Userbasearray);
Businessobj.add ("Users", userbaseobj);
Jsonobject centerobj=new jsonobject ();
Centerobj.addproperty ("Latitude", 37.7861386);
Centerobj.addproperty ("Longitude",-122.4026213);
Jsonobject spanobj=new jsonobject ();
Spanobj.addproperty ("Latitude_delta", 0);
Spanobj.addproperty ("Longitude_delta", 0);
Jsonobject regionobj=new jsonobject ();
Regionobj.add ("center", centerobj);
Regionobj.add ("span", spanobj);
Jsonarray businessesarray=new Jsonarray (); Businessesarray.add (BusinessoBJ);
Obj.add ("Businesses", Businessesarray);
Obj.add ("Region", regionobj);
Obj.addproperty ("Total", 10651);
String Gsonstr=gson.tojson (obj);
System.out.println (GSONSTR);
System.out.println (obj);
public static void Main (string[] args) {Gsonassembled.assemblegson ();
}/** * Example JSON string {"Businesses": [{"Categories": [[] "Local Flavor", "Localflavor"], ["Mass Media", "Massmedia"]], "is_claime D ": True," is_closed ": false," name ":" Yelp "," Phone ":" 4159083801 "," menu_date_updated ": 13174143
"Users": {"user_base": [{"Name": "Yuan", "sex": "Male"},
{"Name": "Wang", "Sex": "Female"}] }], "region": {"center": {"latitude": 37.7861386, "Longitude": -122.4026213}, "span"
: { "Latitude_delta": 0, "Longitude_delta": 0}}, "Total": 10651} * * *