Gson简单使用
2012-07-16 14:34
344 查看
1.简单的处理list和map
Java代码
Gson gson = new Gson();
List testList = new ArrayList();
testList.add("first");
testList.add("second");
String listToJson = gson.toJson(testList);
System.out.println(listToJson);
//prints ["first","second"]
Map testMap = new HashMap();
testMap.put("id", "id.first");
testMap.put("name","name.second");
String mapToJson = gson.toJson(testMap);
System.out.println(mapToJson);
//prints {"id":"id.first","name":"name.second"}
Gson gson = new Gson();
List testList = new ArrayList();
testList.add("first");
testList.add("second");
String listToJson = gson.toJson(testList);
System.out.println(listToJson);
//prints ["first","second"]
Map testMap = new HashMap();
testMap.put("id", "id.first");
testMap.put("name","name.second");
String mapToJson = gson.toJson(testMap);
System.out.println(mapToJson);
//prints {"id":"id.first","name":"name.second"} 2.处理带泛型的集合
Java代码
List<TestBean> testBeanList = new ArrayList<TestBean>();
TestBean testBean = new TestBean();
testBean.setId("id");
testBean.setName("name");
testBeanList.add(testBean);
List<TestBean> testBeanList = new ArrayList<TestBean>();
TestBean testBean = new TestBean();
testBean.setId("id");
testBean.setName("name");
testBeanList.add(testBean);
Java代码
java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<List<TestBean>>() {
}.getType();
String beanListToJson = gson.toJson(testBeanList,type);
System.out.println(beanListToJson);
//prints [{"id":"id","name":"name"}]
List<TestBean> testBeanListFromJson = gson.fromJson(beanListToJson, type);
System.out.println(testBeanListFromJson);
//prints [TestBean@1ea5671[id=id,name=name,birthday=<null>]]
java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<List<TestBean>>() {
}.getType();
String beanListToJson = gson.toJson(testBeanList,type);
System.out.println(beanListToJson);
//prints [{"id":"id","name":"name"}]
List<TestBean> testBeanListFromJson = gson.fromJson(beanListToJson, type);
System.out.println(testBeanListFromJson);
//prints [TestBean@1ea5671[id=id,name=name,birthday=<null>]]map等其他集合类型同上
3.Date类型转化
先写工具类
Java代码
import java.lang.reflect.Type;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
public class UtilDateDeserializer implements JsonDeserializer<java.util.Date> {
@Override
public java.util.Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
return new java.util.Date(json.getAsJsonPrimitive().getAsLong());
}
}
import java.lang.reflect.Type;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
public class UtilDateDeserializer implements JsonDeserializer<java.util.Date> {
@Override
public java.util.Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
return new java.util.Date(json.getAsJsonPrimitive().getAsLong());
}
}
Java代码
import java.lang.reflect.Type;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
public class UtilDateSerializer implements JsonSerializer<java.util.Date> {
@Override
public JsonElement serialize(java.util.Date src, Type typeOfSrc,
JsonSerializationContext context) {
return new JsonPrimitive(src.getTime());
}
}
import java.lang.reflect.Type;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
public class UtilDateSerializer implements JsonSerializer<java.util.Date> {
@Override
public JsonElement serialize(java.util.Date src, Type typeOfSrc,
JsonSerializationContext context) {
return new JsonPrimitive(src.getTime());
}
}
Java代码
/**
* 序列化方法
* @param bean
* @param type
* @return
*/
public static String bean2json(Object bean, Type type) {
Gson gson = new GsonBuilder().registerTypeAdapter(java.util.Date.class, new UtilDateSerializer())
.setDateFormat(DateFormat.LONG).create();
return gson.toJson(bean);
}
/**
* 反序列化方法
* @param json
* @param type
* @return
*/
public static <T> T json2bean(String json, Type type) {
Gson gson = new GsonBuilder().registerTypeAdapter(java.util.Date.class, new UtilDateDeserializer())
.setDateFormat(DateFormat.LONG).create();
return gson.fromJson(json, type);
}
/**
* 序列化方法
* @param bean
* @param type
* @return
*/
public static String bean2json(Object bean, Type type) {
Gson gson = new GsonBuilder().registerTypeAdapter(java.util.Date.class, new UtilDateSerializer())
.setDateFormat(DateFormat.LONG).create();
return gson.toJson(bean);
}
/**
* 反序列化方法
* @param json
* @param type
* @return
*/
public static <T> T json2bean(String json, Type type) {
Gson gson = new GsonBuilder().registerTypeAdapter(java.util.Date.class, new UtilDateDeserializer())
.setDateFormat(DateFormat.LONG).create();
return gson.fromJson(json, type);
}
现在开始测试
Java代码
List<TestBean> testBeanList = new ArrayList<TestBean>();
TestBean testBean = new TestBean();
testBean.setId("id");
testBean.setName("name");
testBean.setBirthday(new java.util.Date());
testBeanList.add(testBean);
java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<List<TestBean>>() {
}.getType();
String beanListToJson = bean2json(testBeanList, type);
System.out.println("beanListToJson:" + beanListToJson);
//prints [{"id":"id","name":"name","birthday":1256531559390}]
List<TestBean> testBeanListFromJson = json2bean(beanListToJson, type);
System.out.println(testBeanListFromJson);
//prints [TestBean@77a7f9[id=id,name=name,birthday=Mon Oct 26 12:39:05 CST 2009]]
List<TestBean> testBeanList = new ArrayList<TestBean>();
TestBean testBean = new TestBean();
testBean.setId("id");
testBean.setName("name");
testBean.setBirthday(new java.util.Date());
testBeanList.add(testBean);
java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<List<TestBean>>() {
}.getType();
String beanListToJson = bean2json(testBeanList, type);
System.out.println("beanListToJson:" + beanListToJson);
//prints [{"id":"id","name":"name","birthday":1256531559390}]
List<TestBean> testBeanListFromJson = json2bean(beanListToJson, type);
System.out.println(testBeanListFromJson);
//prints [TestBean@77a7f9[id=id,name=name,birthday=Mon Oct 26 12:39:05 CST 2009]]
后记:对于java.sql.Date的转化同上类似,写两个类用于其序列化和反序列化即可SQLDateDeserializer implements JsonDeserializer<java.sql.Date>
SQLDateSerializer implements JsonSerializer<java.sql.Date>
转载于:http://qsh123.iteye.com/blog/1181447
Java代码
Gson gson = new Gson();
List testList = new ArrayList();
testList.add("first");
testList.add("second");
String listToJson = gson.toJson(testList);
System.out.println(listToJson);
//prints ["first","second"]
Map testMap = new HashMap();
testMap.put("id", "id.first");
testMap.put("name","name.second");
String mapToJson = gson.toJson(testMap);
System.out.println(mapToJson);
//prints {"id":"id.first","name":"name.second"}
Gson gson = new Gson();
List testList = new ArrayList();
testList.add("first");
testList.add("second");
String listToJson = gson.toJson(testList);
System.out.println(listToJson);
//prints ["first","second"]
Map testMap = new HashMap();
testMap.put("id", "id.first");
testMap.put("name","name.second");
String mapToJson = gson.toJson(testMap);
System.out.println(mapToJson);
//prints {"id":"id.first","name":"name.second"} 2.处理带泛型的集合
Java代码
List<TestBean> testBeanList = new ArrayList<TestBean>();
TestBean testBean = new TestBean();
testBean.setId("id");
testBean.setName("name");
testBeanList.add(testBean);
List<TestBean> testBeanList = new ArrayList<TestBean>();
TestBean testBean = new TestBean();
testBean.setId("id");
testBean.setName("name");
testBeanList.add(testBean);
Java代码
java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<List<TestBean>>() {
}.getType();
String beanListToJson = gson.toJson(testBeanList,type);
System.out.println(beanListToJson);
//prints [{"id":"id","name":"name"}]
List<TestBean> testBeanListFromJson = gson.fromJson(beanListToJson, type);
System.out.println(testBeanListFromJson);
//prints [TestBean@1ea5671[id=id,name=name,birthday=<null>]]
java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<List<TestBean>>() {
}.getType();
String beanListToJson = gson.toJson(testBeanList,type);
System.out.println(beanListToJson);
//prints [{"id":"id","name":"name"}]
List<TestBean> testBeanListFromJson = gson.fromJson(beanListToJson, type);
System.out.println(testBeanListFromJson);
//prints [TestBean@1ea5671[id=id,name=name,birthday=<null>]]map等其他集合类型同上
3.Date类型转化
先写工具类
Java代码
import java.lang.reflect.Type;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
public class UtilDateDeserializer implements JsonDeserializer<java.util.Date> {
@Override
public java.util.Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
return new java.util.Date(json.getAsJsonPrimitive().getAsLong());
}
}
import java.lang.reflect.Type;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
public class UtilDateDeserializer implements JsonDeserializer<java.util.Date> {
@Override
public java.util.Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
return new java.util.Date(json.getAsJsonPrimitive().getAsLong());
}
}
Java代码
import java.lang.reflect.Type;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
public class UtilDateSerializer implements JsonSerializer<java.util.Date> {
@Override
public JsonElement serialize(java.util.Date src, Type typeOfSrc,
JsonSerializationContext context) {
return new JsonPrimitive(src.getTime());
}
}
import java.lang.reflect.Type;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
public class UtilDateSerializer implements JsonSerializer<java.util.Date> {
@Override
public JsonElement serialize(java.util.Date src, Type typeOfSrc,
JsonSerializationContext context) {
return new JsonPrimitive(src.getTime());
}
}
Java代码
/**
* 序列化方法
* @param bean
* @param type
* @return
*/
public static String bean2json(Object bean, Type type) {
Gson gson = new GsonBuilder().registerTypeAdapter(java.util.Date.class, new UtilDateSerializer())
.setDateFormat(DateFormat.LONG).create();
return gson.toJson(bean);
}
/**
* 反序列化方法
* @param json
* @param type
* @return
*/
public static <T> T json2bean(String json, Type type) {
Gson gson = new GsonBuilder().registerTypeAdapter(java.util.Date.class, new UtilDateDeserializer())
.setDateFormat(DateFormat.LONG).create();
return gson.fromJson(json, type);
}
/**
* 序列化方法
* @param bean
* @param type
* @return
*/
public static String bean2json(Object bean, Type type) {
Gson gson = new GsonBuilder().registerTypeAdapter(java.util.Date.class, new UtilDateSerializer())
.setDateFormat(DateFormat.LONG).create();
return gson.toJson(bean);
}
/**
* 反序列化方法
* @param json
* @param type
* @return
*/
public static <T> T json2bean(String json, Type type) {
Gson gson = new GsonBuilder().registerTypeAdapter(java.util.Date.class, new UtilDateDeserializer())
.setDateFormat(DateFormat.LONG).create();
return gson.fromJson(json, type);
}
现在开始测试
Java代码
List<TestBean> testBeanList = new ArrayList<TestBean>();
TestBean testBean = new TestBean();
testBean.setId("id");
testBean.setName("name");
testBean.setBirthday(new java.util.Date());
testBeanList.add(testBean);
java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<List<TestBean>>() {
}.getType();
String beanListToJson = bean2json(testBeanList, type);
System.out.println("beanListToJson:" + beanListToJson);
//prints [{"id":"id","name":"name","birthday":1256531559390}]
List<TestBean> testBeanListFromJson = json2bean(beanListToJson, type);
System.out.println(testBeanListFromJson);
//prints [TestBean@77a7f9[id=id,name=name,birthday=Mon Oct 26 12:39:05 CST 2009]]
List<TestBean> testBeanList = new ArrayList<TestBean>();
TestBean testBean = new TestBean();
testBean.setId("id");
testBean.setName("name");
testBean.setBirthday(new java.util.Date());
testBeanList.add(testBean);
java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<List<TestBean>>() {
}.getType();
String beanListToJson = bean2json(testBeanList, type);
System.out.println("beanListToJson:" + beanListToJson);
//prints [{"id":"id","name":"name","birthday":1256531559390}]
List<TestBean> testBeanListFromJson = json2bean(beanListToJson, type);
System.out.println(testBeanListFromJson);
//prints [TestBean@77a7f9[id=id,name=name,birthday=Mon Oct 26 12:39:05 CST 2009]]
后记:对于java.sql.Date的转化同上类似,写两个类用于其序列化和反序列化即可SQLDateDeserializer implements JsonDeserializer<java.sql.Date>
SQLDateSerializer implements JsonSerializer<java.sql.Date>
转载于:http://qsh123.iteye.com/blog/1181447
相关文章推荐
- 简单的Gson使用
- [Android | 小代码] 天气预报 简易Demo json解析数组+对象 Gson+Volley简单使用
- Gson的简单使用(二)
- Android建立网络连接,利用JSON数据获取百度图片搜索结果及GSON的简单使用
- Gson的简单使用
- Gson简单使用
- Gson的简单使用(一)
- Gson的简单使用
- Gson简单使用
- gson简单使用及下载地址
- Gson的简单使用
- Gson的简单使用
- Gson的简单使用
- kotlin简单小demo,请求,解析,gson的使用,volley的单例
- Android杂谈(14)OKHttp+Gson的简单使用
- Retrofit2.0的简单使用 (包含Gson)
- gson 的简单使用
- okhttp框架的最简单的基本使用 post和get请求以及用gson解析请求的数据
- Gson的简单使用(待补充)
- Java Json API:Gson使用简单入门