Gson-list和array以及Vector

将List转为JSON字符串
示例代码如下

    static Gson sGson;

    static Gson getGson() {
        if (sGson == null) {
            sGson = getGsonBuilder().create();
        }
        return sGson;
    }

    private static GsonBuilder getGsonBuilder() {
        return new GsonBuilder();
    }

    private static void testArrayOrList(){
        List list = new ArrayList();
        for (int i =0;i<3;i++){
            list.add( new Empolyee("longxia", i,10d+i));
        }
        String result = getGson().toJson(list);
        System.out.println(result);
        result = getGson().toJson(list,list.getClass());
        System.out.println(result);
        result = getGson().toJson(list,new TypeToken<List<Empolyee>>(){}.getType());
        System.out.println(result);

    }
    public static void main(String[] args) {
        testArrayOrList();

    }

    static class Empolyee {
        String name;
        float department;
        Double score;

        public Empolyee(String name, float department, Double score) {
            this.name = name;
            this.department = department;
            this.score = score;
        }
    }

输出结果为

[{"name":"longxia","department":0.0,"score":10.0},{"name":"longxia","department":1.0,"score":11.0},{"name":"longxia","department":2.0,"score":12.0}]
[{"name":"longxia","department":0.0,"score":10.0},{"name":"longxia","department":1.0,"score":11.0},{"name":"longxia","department":2.0,"score":12.0}]
[{"name":"longxia","department":0.0,"score":10.0},{"name":"longxia","department":1.0,"score":11.0},{"name":"longxia","department":2.0,"score":12.0}]

这里的三种写法都可以正常输出JSON字符串,本质上第一个方法里面实际调用的是第二个方法,第二个方法和第三个方法实际是一个方法。getClass()可以强转为Type,所以直接用第一个就行。
Vector转换为JSON的示例代码

        Vector vector = new Vector(3);
        for (int i =0;i<3;i++){
            vector.add(new Empolyee("longxia", i,10d+i));
        }
        String result = getGson().toJson(vector);
        System.out.println(result);
        result = getGson().toJson(vector,vector.getClass());
        System.out.println(result);
        result = getGson().toJson(vector,new TypeToken<Vector<Empolyee>>(){}.getType());
        System.out.println(result);

输出结果为:

[{"name":"longxia","department":0.0,"score":10.0},{"name":"longxia","department":1.0,"score":11.0},{"name":"longxia","department":2.0,"score":12.0}]
[{"name":"longxia","department":0.0,"score":10.0},{"name":"longxia","department":1.0,"score":11.0},{"name":"longxia","department":2.0,"score":12.0}]
[{"name":"longxia","department":0.0,"score":10.0},{"name":"longxia","department":1.0,"score":11.0},{"name":"longxia","department":2.0,"score":12.0}]

输出结果和list一样,都是带有[]中括号,都是数组的形式
下面看下他们的反序列

    private static void testDesArrayListAndVector(){
        String srcStr="[{\"name\":\"longxia\",\"department\":0.0,\"score\":10.0},"
                + "{\"name\":\"longxia\",\"department\":1.0,\"score\":11.0},"
                + "{\"name\":\"longxia\",\"department\":2.0,\"score\":12.0}]";
        List<Empolyee> empolyees = new ArrayList<>();
        empolyees = getGson().fromJson(srcStr,empolyees.getClass());
        for (int i =0;i<empolyees.size();i++){
            System.out.println(empolyees.get(i));
        }
        empolyees = getGson().fromJson(srcStr,new TypeToken<List<Empolyee>>(){}.getType());
        for (int i =0;i<empolyees.size();i++){
            System.out.println(empolyees.get(i));
        }
    }

这两种写法的输出结果分别为:

{name=longxia, department=0.0, score=10.0}
{name=longxia, department=1.0, score=11.0}
{name=longxia, department=2.0, score=12.0}
com.company.util.GsonUtil$Empolyee@26a7b76d
com.company.util.GsonUtil$Empolyee@4abdb505
com.company.util.GsonUtil$Empolyee@7ce6a65d

我们可以看到用
fromJson(String json, Class classOfT)可以输出第一种,貌似输出的是格式化后的。
但是用fromJson(String json, Type typeOfT) 方法不能格式化输出。
查看源码发现fromJson(String json, Class classOfT)本质调用了fromJson(String json, Type typeOfT) 方法,但是比第二个多了个"return Primitives.wrap(classOfT).cast(object);"目前看源码不知道里面做了啥???

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值