使用Gson我們可以很方便把一個自訂Java對象轉換為Gson。但是,我們也需要知道一些Java基本對象和Gson之間的轉換,我們這裡就說一些基本Java對象和Gson直接的轉換。
1、構建JsonObject對象
Json字串中,如果想要顯示出來是 { } 這樣格式的,就必須構建JsonObject對象
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有2種add方法,一種是addProperty,這個方法的第一個參數是Key,第二個參數是Value,Key只能是String類型的,Value可以是Boolean,Integer,String 等Java基本類型
第二種方法是add,add方法也有2個參數,分別作為Key,Value,其中Value是JsonElement對象,其中JsonElement有一些子類,像:JsonArray, JsonNull, JsonObject, JsonPrimitive這些都是JsonElement的子類
2、構建JsonArray、JsonElement對象
在Json字串中,如果想要顯示出來是 [ ] 這樣格式的,就必須構建一個JsonArray對象或者JsonElement對象
JsonArray的add方法只能接受JsonElement或其子類,所以我們需要先構建一個JsonElement或JsonObject等
eg:
JsonArray businessesArray=new JsonArray();
businessesArray.add(businessObj);//businessObj為一個JsonObject對象
因為JsonElement沒有顯式的構造方法,我們如果想要構建JsonArray類型的JsonElement時,需要借用Gson的toJsonTree方法,該方法接受一個Object對象,我們可以用任意的Java對象來構建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、產生JSON字串
如果需要輸出JSON字串,第一種方法是構建一個JsonObject對象,然後輸出JsonObject對象即可,第二種是用Gson的toJson方法,產生一個String類型的Json串
eg:(其中Obj是一個JsonObject對象)
String gsonStr=gson.toJson(obj);
System.out.println(gsonStr);
System.out.println(obj);
4、樣本
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_claimed": true, "is_closed": false, "name": "Yelp", "phone": "4159083801", "menu_date_updated": 1317414369, "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} * */