码迷,mamicode.com
首页 > 其他好文 > 详细

GSON的基本用法三

时间:2020-05-13 12:32:32      阅读:59      评论:0      收藏:0      [点我收藏+]

标签:oid   one   substring   date   google   sub   用法   from   截取   

演示gson数组的序列化和反序列化。


import com.google.gson.Gson;


/**
 * @author yongjar
 * @date 2020/4/28
 */
public class GsonTester {

    public static void main(String args[]) {




        // 演示序列化
        Gson gson = new Gson();


        int[] ints = {1,9,5,8,7};
        System.out.println("案例1:" + gson.toJson(ints));


        String[] strings = {"123", "456", "789"};
        System.out.println("案例2:" + gson.toJson(strings));



        // 演示反序列化
        int[] ints2 = gson.fromJson("[1,2,3,4,5]", int[].class);

        String arr="";
        for (int i = 0; i <ints2.length ; i++) {

            arr+=ints2[i] + ",";

        }


        //这里的substring方法是截取最后一个逗号
        System.out.println("案例3:" + arr.substring(0,arr.length()-1));




    }

}


GSON的基本用法三

标签:oid   one   substring   date   google   sub   用法   from   截取   

原文地址:https://www.cnblogs.com/jamal/p/12881191.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!