google gson学习

本文介绍了如何使用Gson库处理Java中的List、Map及带有日期类型的复杂对象的序列化与反序列化过程。包括简单类型的转换、带泛型的集合处理以及自定义日期格式的序列化与反序列化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Gson网址http://code.google.com/p/google-gson/

 

1.简单的处理list和map

		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.处理带泛型的集合

		List<TestBean> testBeanList = new ArrayList<TestBean>();
		TestBean testBean = new TestBean();
		testBean.setId("id");
		testBean.setName("name");
		testBeanList.add(testBean);
		
		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类型转化

 

先写工具类

 

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.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());   
    }   

}

 

/**
	 * 序列化方法
	 * @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);
	}

 

 

现在开始测试

 

		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>

 

 

 对于GsonBuilder详见:

 http://google-gson.googlecode.com/svn/tags/1.2.3/docs/javadocs/com/google/gson/GsonBuilder.html

 

 

*注:TestBean就是一个带id、name、birthday的javabean,就不在这里贴代码了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值