Gson具体用法
来源:互联网 发布:ue设计软件 编辑:程序博客网 时间:2024/06/11 02:27
来源:http://xuexizhishen.blog.51cto.com/2687197/744910
package com.lupeng.javase.json.bean;import java.io.Serializable;/** * 普通JavaBean类 * @author 翔林小刚 * @date 2011-12-18 */public class JavaBean implements Serializable{ private static final long serialVersionUID = 8720431164344424704L; private String id; private String name; private int age; private String addr; public JavaBean() { super(); } public JavaBean(String id, String name, int age, String addr) { super(); this.id = id; this.name = name; this.age = age; this.addr = addr; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddr() { return addr; } public void setAddr(String addr) { this.addr = addr; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", age=" + age + ", addr=" + addr + "]"; }}-------------------------------------------------无情分割线--------------------------------------------package com.lupeng.javase.json.bean;import java.io.Serializable;import java.util.Date;/** * 带日期属性的JavaBean类 * @author 翔林小刚 * @date 2011-12-18 */public class DateBean implements Serializable { private static final long serialVersionUID = -2071405788146467301L; private String id; private String name; private int age; private Date birth; public DateBean() { super(); } public DateBean(String id, String name, int age, Date birth) { super(); this.id = id; this.name = name; this.age = age; this.birth = birth; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } @Override public String toString() { return "DateBean [age=" + age + ", birth=" + birth + ", id=" + id + ", name=" + name + "]"; }}-------------------------------------------------无情分割线--------------------------------------------package com.lupeng.javase.json.util;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;/** * 日期序列化实用工具类 * @author 翔林小刚 * @date 2011-12-18 */public class DateDeserializerUtils implements JsonDeserializer<java.util.Date>{ @Override public java.util.Date deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { return new java.util.Date(json.getAsJsonPrimitive().getAsLong()); }}-------------------------------------------------无情分割线--------------------------------------------package com.lupeng.javase.json.util;import java.lang.reflect.Type;import java.util.Date;import com.google.gson.JsonElement;import com.google.gson.JsonPrimitive;import com.google.gson.JsonSerializationContext;import com.google.gson.JsonSerializer;/** * 日期解序列实用工具类 * @author 翔林小刚 * @date 2011-12-18 */public class DateSerializerUtils implements JsonSerializer<java.util.Date>{ @Override public JsonElement serialize(Date date, Type type, JsonSerializationContext content) { return new JsonPrimitive(date.getTime()); }}-------------------------------------------------无情分割线--------------------------------------------package com.lupeng.javase.apps.json;import java.text.DateFormat;import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.List;import java.util.Map;import org.junit.Before;import org.junit.Test;import com.google.gson.Gson;import com.google.gson.GsonBuilder;import com.lupeng.javase.json.bean.DateBean;import com.lupeng.javase.json.bean.JavaBean;import com.lupeng.javase.json.util.DateDeserializerUtils;import com.lupeng.javase.json.util.DateSerializerUtils;/** * Google Gson解析Json数据实例 * * 1.Bean、Json转换 testBeanJson() 2.List -> Json转换 testList2Json() * 3.泛型List、Json相互转换 testGenericList2Json() 4.Map -> Json转换 testMap2Json() * 5.泛型Map、Json相互转换 testGenericMap2Json() 6.带日期属性Bean、Json转换 testDateBeanJson() * 7.带日期属性泛型List、Json转换 testDateGenericListJson() * * @author 翔林小刚 * @create 2011-08-05 * @modify 2011-12-18 */@SuppressWarnings("unchecked")public class GsonTester { private Gson gson = null; private GsonBuilder gsonBuilder = null; @Before public void setUp() { gson = new Gson(); gsonBuilder = new GsonBuilder(); } /** * JavaBean、Json相互转换 */ @Test public void testBeanJson() { JavaBean bean = new JavaBean("1001", "scott", 20, "TL"); // Bean -> Json String json = gson.toJson(bean); System.out.println(json); // Json -> Bean bean = gson.fromJson(json, JavaBean.class); System.out.println(bean); } /** * List转换成Json字符串 */ @Test public void testList2Json() { // List List list = new ArrayList(); for (int i = 0; i < 5; i++) { list.add("element" + i); } System.out.println(list); // List -> Json String json = gson.toJson(list); System.out.println(json); } /** * 泛型List、Json相互转换 */ @Test public void testGenericListJson() { // 泛型List List<JavaBean> list = new ArrayList<JavaBean>(); for (int i = 0; i < 3; i++) { JavaBean user = new JavaBean("100" + i, "name" + i, 20 + i, "BJ" + i); list.add(user); } System.out.println(list); // 泛型List -> Json java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<List<JavaBean>>() { }.getType(); String json = gson.toJson(list, type); System.out.println(json); // Json -> 泛型List List<JavaBean> users = gson.fromJson(json.toString(), type); System.out.println(users); } /** * Map转换成Json字符串 */ @Test public void testMap2Json() { // Map数据 Map map = new HashMap(); map.put("id", "1001"); map.put("name", "scott"); map.put("age", 20); map.put("addr", "BJ"); System.out.println(map); // Map -> Json String json = gson.toJson(map); System.out.println(json); } /** * 泛型Map、Json相互转换 */ @Test public void testGenericMapJson() { // 泛型Map数据 Map<String, JavaBean> map = new HashMap<String, JavaBean>(); for (int i = 0; i < 5; i++) { JavaBean user = new JavaBean("100" + i, "name" + i, 20 + i, "LN" + i); map.put("100" + i, user); } System.out.println(map); // 泛型Map -> Json java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<Map<String, JavaBean>>() { }.getType(); String json = gson.toJson(map, type); System.out.println(json); // Json -> Map Map<String, JavaBean> users = gson.fromJson(json.toString(), type); System.out.println(users); } /** * 带日期类型Bean、Json相互转换 */ @Test public void testDateBeanJson() { // 日期Bean数据 DateBean bean = new DateBean("1001", "scott", 20, new Date()); // Bean(带日期属性) -> Json gson = gsonBuilder.registerTypeAdapter(java.util.Date.class, new DateSerializerUtils()).setDateFormat(DateFormat.LONG) .create(); // gson = new // GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create(); String json = gson.toJson(bean); System.out.println("_________________________" + json); // Json -> Bean(带日期类型属性) gson = gsonBuilder.registerTypeAdapter(java.util.Date.class,new DateDeserializerUtils()).setDateFormat("yyyy/MM/dd") .create(); java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<DateBean>() { }.getType(); DateBean b = gson.fromJson(json, type); System.out.println(b); } /** * 泛型日期List、Json相互转换 */ @Test public void testDateGenericListJson() { // 泛型日期List List<DateBean> list = new ArrayList<DateBean>(); for (int i = 0; i < 3; i++) { DateBean user = new DateBean("100" + i, "name" + i, 20 + i, new Date()); list.add(user); } System.out.println(list); // 泛型日期List -> Json gson = gsonBuilder.registerTypeAdapter(java.util.Date.class, new DateSerializerUtils()).setDateFormat(DateFormat.LONG) .create(); java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<List<DateBean>>() { }.getType(); String json = gson.toJson(list, type); System.out.println(json); // Json -> 泛型日期List gson = gsonBuilder.registerTypeAdapter(java.util.Date.class, new DateDeserializerUtils()).setDateFormat(DateFormat.LONG) .create(); List<DateBean> users = gson.fromJson(json.toString(), type); System.out.println(users); }}
0 0
- Gson具体用法
- Gson用法
- Gson的具体使用总结
- Gson的简要用法
- Gson的基本用法
- Android Gson用法
- Android Gson用法
- gson常用用法小结
- Gson的简单用法
- Gson的用法
- Gson用法1
- gson常用用法小结
- Gson用法详解
- GSON的用法
- Gson用法整理
- Gson 常用用法
- Gson用法整理
- Gson用法一
- log4j使用
- eclipse下生成Java类图和时序图,生成UML图
- 整数和字符型的转换
- VARCHAR和NVARCHAR的区别
- jni 打印堆栈方法
- Gson具体用法
- Android webview详解
- unity3d 关于FindObjectsOfType和FindObjectOfType 之遍历对象
- Git分支管理策略
- 计算机类期刊
- SRM 144DIV2 200 C++
- 玩转nodeJS系列:使用cluster创建nodejs单机多核集群(多进程)
- Linux常用命令汇总
- 写入orauts.dll 时出错