注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

一线天色 天宇星辰

天下武功,唯快不破

 
 
 

日志

 
 

解析生成JSON利器—GSON(二)  

2012-06-13 20:53:29|  分类: 软件开发 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

注:因网易博文字数限制,分为两部分,上接解析生成JSON利器—GSON(一)

四、列表(List)形式与JSON互转

示例代码:

package com.yhj.gson;

 

import java.io.Serializable;

import java.util.ArrayList;

import java.util.Date;

import java.util.List;

 

import com.google.gson.Gson;

import com.google.gson.reflect.TypeToken;

/**

 * @Described:容器测试用例

 * @author YHJ create at 2012-6-11 下午09:12:27

 * @FileNmae com.yhj.gson.ListTestCase.java

 */

public class ListTestCase implements Serializable {

 

    /**

     * @FieldInfo ListTestCase.javalong

     * @Author YHJ create at 2012-6-11 下午09:12:25

     */

    private static final long serialVersionUID = 933765965334124420L;

   

    private  List<Integer> ints = new ArrayList<Integer>();

    private  List<String> strs = new ArrayList<String>();

    private  List<Date> dates = new ArrayList<Date>();

    private  List<ClassTestCase> casesClassTestCases = new ArrayList<ClassTestCase>();

    private  List<InnerClassClassTestCase> innerClassClassTestCases = new ArrayList<InnerClassClassTestCase>();

    private  List<ClassTestCase> nullClass;

    public ListTestCase() {

       ints.add(1);

       ints.add(2);

       ints.add(3);

      

       strs.add("4");

       strs.add("5");

       strs.add("6");

      

       dates.add(new Date());

       dates.add(new Date());

      

       casesClassTestCases.add(new ClassTestCase());

      

       innerClassClassTestCases.add(new InnerClassClassTestCase());

    }

   

   

    public List<Integer> getInts() {

       return ints;

    }

    public void setInts(List<Integer> ints) {

       this.ints = ints;

    }

    public List<String> getStrs() {

       return strs;

    }

    public void setStrs(List<String> strs) {

       this.strs = strs;

    }

    public List<Date> getDates() {

       return dates;

    }

    public void setDates(List<Date> dates) {

       this.dates = dates;

    }

    public List<ClassTestCase> getCasesClassTestCases() {

       return casesClassTestCases;

    }

    public void setCasesClassTestCases(List<ClassTestCase> casesClassTestCases) {

       this.casesClassTestCases = casesClassTestCases;

    }

    public List<InnerClassClassTestCase> getInnerClassClassTestCases() {

       return innerClassClassTestCases;

    }

    public void setInnerClassClassTestCases(

           List<InnerClassClassTestCase> innerClassClassTestCases) {

       this.innerClassClassTestCases = innerClassClassTestCases;

    }

    public List<ClassTestCase> getNullClass() {

       return nullClass;

    }

    public void setNullClass(List<ClassTestCase> nullClass) {

       this.nullClass = nullClass;

    }

 

 

    //主函数

    public static void main(String[] args) {

       Gson gson = new Gson();

       ListTestCase listTestCase = new ListTestCase();

       System.out.println(gson.toJson(listTestCase));

       listTestCase.getInnerClassClassTestCases().clear();

       System.out.println(gson.toJson(listTestCase));

       String source = "{\"ints\":[1,2,3],\"strs\":[\"4\",\"5\",\"6\"],\"dates\":[\"Jun 11, 2012 9:20:28 PM\",\"Jun 11, 2012 9:20:28 PM\"],\"casesClassTestCases\":[{\"id\":10010,\"name\":\"中国联通\",\"age\":30,\"sex\":\"股份国企\",\"cellphone\":\"100861\",\"birthday\":\"Jun 11, 2012 9:20:28 PM\"}],\"innerClassClassTestCases\":[]}";

       ListTestCase listTestCase2 = gson.fromJson(source, ListTestCase.class);

       System.out.println(listTestCase2);

       System.out.println(listTestCase2.getInts());

       System.out.println(listTestCase2.getStrs());

       System.out.println(listTestCase2.getDates());

       System.out.println(listTestCase2.getCasesClassTestCases());

       System.out.println(listTestCase2.getInnerClassClassTestCases());

       System.out.println(listTestCase2.getInnerClassClassTestCases().size());

       System.out.println(listTestCase2.getNullClass());

       source = "{\"ints\":[1,2,3],\"strs\":[\"4\",\"5\",\"6\"],\"dates\":[\"Jun 11, 2012 9:20:28 PM\",\"Jun 11, 2012 9:20:28 PM\"]}";

       listTestCase2 = gson.fromJson(source, ListTestCase.class);

       System.out.println(listTestCase2);

       System.out.println(listTestCase2.getInts());

       System.out.println(listTestCase2.getStrs());

       System.out.println(listTestCase2.getDates());

       System.out.println(listTestCase2.getCasesClassTestCases());

       System.out.println(listTestCase2.getInnerClassClassTestCases());

       System.out.println(listTestCase2.getInnerClassClassTestCases().size());

       System.out.println(listTestCase2.getNullClass());

       //早期的toJson需要写成

       System.out.println(gson.toJson(listTestCase,ListTestCase.class));

       System.out.println(gson.toJson(listTestCase.getCasesClassTestCases()));

       System.out.println(gson.toJson(listTestCase.getCasesClassTestCases()));

       //早期的toJson需要写成

       System.out.println(gson.toJson(listTestCase.getCasesClassTestCases(),(new TypeToken<List<ClassTestCase>>(){}).getType()));

       source = "[{\"id\":10010,\"name\":\"中国联通\",\"age\":30,\"sex\":\"股份国企\",\"cellphone\":\"100861\",\"birthday\":\"Jun 11, 2012 9:39:10 PM\"}]";

       List<ClassTestCase> classTestCases = gson.fromJson(source,(new TypeToken<List<ClassTestCase>>(){}).getType());

       System.out.println(classTestCases);

       System.out.println(classTestCases.get(0));

    }

   

 

}

运行结果:

{"ints":[1,2,3],"strs":["4","5","6"],"dates":["Jun 13, 2012 3:58:39 PM","Jun 13, 2012 3:58:39 PM"],"casesClassTestCases":[{"id":10086,"name":"中国移动","age":30,"sex":"股份国企","cellphone":"100861","birthday":"Jun 13, 2012 3:58:39 PM"}],"innerClassClassTestCases":[{"length":30,"width":80,"height":40,"innerClass":{"id":10086,"name":"中国移动","age":30,"sex":"股份国企","cellphone":"100861","birthday":"Jun 13, 2012 3:58:39 PM"}}]}

{"ints":[1,2,3],"strs":["4","5","6"],"dates":["Jun 13, 2012 3:58:39 PM","Jun 13, 2012 3:58:39 PM"],"casesClassTestCases":[{"id":10086,"name":"中国移动","age":30,"sex":"股份国企","cellphone":"100861","birthday":"Jun 13, 2012 3:58:39 PM"}],"innerClassClassTestCases":[]}

com.yhj.gson.ListTestCase@1bfc93a

[1, 2, 3]

[4, 5, 6]

[Mon Jun 11 21:20:28 CST 2012, Mon Jun 11 21:20:28 CST 2012]

[com.yhj.gson.ClassTestCase@15b0afd]

[]

0

null

com.yhj.gson.ListTestCase@2e7820

[1, 2, 3]

[4, 5, 6]

[Mon Jun 11 21:20:28 CST 2012, Mon Jun 11 21:20:28 CST 2012]

[com.yhj.gson.ClassTestCase@d08633]

[com.yhj.gson.InnerClassClassTestCase@1a9334]

1

null

{"ints":[1,2,3],"strs":["4","5","6"],"dates":["Jun 13, 2012 3:58:39 PM","Jun 13, 2012 3:58:39 PM"],"casesClassTestCases":[{"id":10086,"name":"中国移动","age":30,"sex":"股份国企","cellphone":"100861","birthday":"Jun 13, 2012 3:58:39 PM"}],"innerClassClassTestCases":[]}

[{"id":10086,"name":"中国移动","age":30,"sex":"股份国企","cellphone":"100861","birthday":"Jun 13, 2012 3:58:39 PM"}]

[{"id":10086,"name":"中国移动","age":30,"sex":"股份国企","cellphone":"100861","birthday":"Jun 13, 2012 3:58:39 PM"}]

[{"id":10086,"name":"中国移动","age":30,"sex":"股份国企","cellphone":"100861","birthday":"Jun 13, 2012 3:58:39 PM"}]

[com.yhj.gson.ClassTestCase@15212bc]

com.yhj.gson.ClassTestCase@15212bc

 

五、Map类型的与JSON互转

示例代码:

package com.yhj.gson;

 

import java.io.Serializable;

import java.util.HashMap;

import java.util.Map;

 

import com.google.gson.Gson;

import com.google.gson.reflect.TypeToken;

/**

 * @DescribedMap类型的互转

 * @author YHJ create at 2012-6-11 下午09:34:51

 * @FileNmae com.yhj.gson.MapTestCase.java

 */

public class MapTestCase implements Serializable {

 

    /**

     * @FieldInfo MapTestCase.javalong

     * @Author YHJ create at 2012-6-11 下午09:34:48

     */

    private static final long serialVersionUID = -3245483494772122182L;

   

    private Map<String, ClassTestCase> mapTestCase = new HashMap<String, ClassTestCase>();

   

    public MapTestCase() {

       mapTestCase.put("NO1", new ClassTestCase());

       mapTestCase.put("NO2", new ClassTestCase());

       mapTestCase.put("NO3", new ClassTestCase());

    }

 

    public Map<String, ClassTestCase> getMapTestCase() {

       return mapTestCase;

    }

 

    public void setMapTestCase(Map<String, ClassTestCase> mapTestCase) {

       this.mapTestCase = mapTestCase;

    }

 

    //主函数

    public static void main(String[] args) {

       Map<String, ClassTestCase> mapTestCase = new MapTestCase().getMapTestCase();

       Gson gson = new Gson();

       System.out.println(gson.toJson(mapTestCase));

       String source = "{\"NO1\":{\"id\":10086,\"name\":\"中国移动\",\"age\":30,\"sex\":\"股份国企\",\"cellphone\":\"100861\",\"birthday\":\"Jun 11, 2012 9:43:28 PM\"},\"NO2\":{\"id\":10086,\"name\":\"中国移动\",\"age\":30,\"sex\":\"股份国企\",\"cellphone\":\"100861\",\"birthday\":\"Jun 11, 2012 9:43:28 PM\"},\"NO3\":{\"id\":10086,\"name\":\"中国移动\",\"age\":30,\"sex\":\"股份国企\",\"cellphone\":\"100861\",\"birthday\":\"Jun 11, 2012 9:43:28 PM\"}}";

       Map<String, ClassTestCase> classTestCases = gson.fromJson(source, (new TypeToken<Map<String, ClassTestCase>>(){}).getType());

       System.out.println(classTestCases);

       System.out.println(classTestCases.get("NO2"));

    }

}

运行结果:

{"NO3":{"id":10086,"name":"中国移动","age":30,"sex":"股份国企","cellphone":"100861","birthday":"Jun 13, 2012 7:50:08 PM"},"NO2":{"id":10086,"name":"中国移动","age":30,"sex":"股份国企","cellphone":"100861","birthday":"Jun 13, 2012 7:50:08 PM"},"NO1":{"id":10086,"name":"中国移动","age":30,"sex":"股份国企","cellphone":"100861","birthday":"Jun 13, 2012 7:50:08 PM"}}

{NO1=com.yhj.gson.ClassTestCase@ce5b1c, NO2=com.yhj.gson.ClassTestCase@1bfc93a, NO3=com.yhj.gson.ClassTestCase@341960}

com.yhj.gson.ClassTestCase@1bfc93a

 

六、带有排除列表的JSON生成

示例代码:

package com.yhj.gson;

 

import java.io.Serializable;

 

import com.google.gson.Gson;

import com.google.gson.GsonBuilder;

import com.google.gson.annotations.Expose;

 

/**

 * @Described:带有排除字段的测试用例

 * @author YHJ create at 2012-6-13 下午07:59:43

 * @FileNmae com.yhj.gson.ClassWithExclusion.java

 */

public class ClassWithExclusionTestCase implements Serializable{

 

    /**

     * @FieldInfo ClassWithExclusionTestCase.javalong

     * @Author YHJ create at 2012-6-13 下午08:33:03

     */

    private static final long serialVersionUID = 1187720451203167089L;

    @Expose

    private String name = "YHJ";

    private String sex = "male";

    @Expose

    private String sign = "http://yhjhappy234.blog.163.com/";

    public String getName() {

       return name;

    }

    public void setName(String name) {

       this.name = name;

    }

    public String getSex() {

       return sex;

    }

    public void setSex(String sex) {

       this.sex = sex;

    }

    public String getSign() {

       return sign;

    }

    public void setSign(String sign) {

       this.sign = sign;

    }

   

    @Override

    public String toString() {

       return "[name:"+name+",sex:"+sex+",sign:"+sign+"]";

    }

   

    //主函数入口

    public static void main(String[] args) {

       Gson gson1 = new Gson();

       Gson gson2 = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();

       System.out.println(gson1.toJson(new ClassWithExclusionTestCase()));

       System.out.println(gson2.toJson(new ClassWithExclusionTestCase()));

       String source1 = "{\"name\":\"YHJ_2012\",\"sex\":\"male_2012\",\"sign\":\"http://yhjhappy234.blog.163.com/2012\"}";

       String source2 = "{\"name\":\"YHJ_2012\",\"sign\":\"http://yhjhappy234.blog.163.com/2012\"}";

       ClassWithExclusionTestCase testCase1 = gson2.fromJson(source1, ClassWithExclusionTestCase.class);

       ClassWithExclusionTestCase testCase2 = gson2.fromJson(source2, ClassWithExclusionTestCase.class);

       System.out.println(testCase1);

       System.out.println(testCase2);

    }

}

运行结果:

{"name":"YHJ","sex":"male","sign":"http://yhjhappy234.blog.163.com/"}

{"name":"YHJ","sign":"http://yhjhappy234.blog.163.com/"}

[name:YHJ_2012,sex:male,sign:http://yhjhappy234.blog.163.com/2012]

[name:YHJ_2012,sex:male,sign:http://yhjhappy234.blog.163.com/2012]

我们看到,gson2生成的json对象已经将没有注解的sex忽略了!当JSON生成对象时,我们将每个对象的数据上加了2012的拼接串,但是可以明显看出,sex并没有加上2012,说明当生成对象时,也没有将新的数据赋值于对应的对象!从而达到排除没有加注解字段的目的!

好了,讲了这么多,相信大家已经可以轻松自如的实现对象与JSON的互转了,当然,GSON这个包还有更强大的功能,具体还有什么呢?建议大家参考一下API,没有的可以留言索取哦!(本人已生成CHM格式的,如有需要,可留言留下邮箱)

  评论这张
 
阅读(64525)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017