最近开始接触使用Google Gson Library,个人觉得还是挺好用的,在这里简单介绍一下开发中 Gson的常用功能!
1.Serialize JSON
package com.ricky.java.gson;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.ricky.java.gson.bean.User;
/**
* How to Serialize and Deserialize JSON Data in Java
* @author Ricky
*
*/
public class GsonSerilize {
public static void main(String[] args) {
serializeObject();
serializeList();
}
public static void serializeObject() {
User user = new User();
user.setId(1001);
// user.setName("Ricky");
user.setAge(25);
Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls().create();
System.out.println(gson.toJson(user));
}
public static void serializeList() {
List<User> list = new ArrayList<User>();
User user = new User();
user.setId(1001);
user.setName("Ricky");
user.setAge(25);
list.add(user);
User user2 = new User();
user2.setId(1002);
user2.setName("Tim");
user2.setAge(24);
list.add(user2);
Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls().create();
System.out.println(gson.toJson(list));
}
}
这里主要用到了
可以方便的序列化Java对象,如 Bean、List、Map对象等等。
package com.ricky.java.gson;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.ricky.java.gson.bean.User;
/**
* How to Serialize and Deserialize JSON Data in Java
* @author Ricky
*
*/
public class GsonDeserilize {
public static void main(String[] args) {
deserializeObject();
deserializeList();
}
public static void deserializeObject() {
String json = "{\"id\":10010,\"name\":\"Ricky Feng\",\"age\":25}";
Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls().create();
User user = gson.fromJson(json, User.class);
System.out.println(user);
}
public static void deserializeList() {
String json = "[{\"id\":10010,\"name\":\"Ricky Feng\",\"age\":25},{\"id\":10011,\"name\":\"Mike\",\"age\":24}]";
Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls().create();
Type listType = new TypeToken<ArrayList<User>>(){}.getType();
List<User> list = gson.fromJson(json, listType);
System.out.println(list);
}
}
这里主要用到了
非常方便的把JSON字符串 反序列化成Java对象。
3.Parse JSON
package com.ricky.java.gson;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
/**
* How to Parse JSON Data in Java With Gson
* @author Ricky
*
*/
public class GsonTest {
public static void main(String[] args) {
String json = "{\"Success\":true,\"Message\":\"Invalid access token.\"}";
//1.JsonParser.parse(String json);
JsonParser jsonParser = new JsonParser();
JsonObject jo = (JsonObject)jsonParser.parse(json);
boolean success = jo.get("Success").getAsBoolean();
System.out.println(success);
String msg = jo.get("Message").getAsString();
System.out.println("msg="+msg);
System.out.println("------------------");
//2.Gson.fromJson(String json, Class<JsonElement> classOfT);
Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls().create();
JsonElement jelem = gson.fromJson(json, JsonElement.class);
JsonObject jobj = jelem.getAsJsonObject(); //jelem.getAsJsonArray();
msg = jobj.get("Message").getAsString();
System.out.println("msg="+msg);
boolean has = jobj.has("Status");
System.out.println("has="+has);
}
}
目前自己接触到的Gson API就这么多了,以后对Gson有更深入的了解会继续更新!