Gson的使用笔记。

1.Gson 介绍。

 Gson是处理json格式工具,联合GsonFormat插件使用,更美味。

2.注解

  • @Expose     制定字段显示。serialize序列化。deserialize反序列化。true为允许。对应false
  • @SerializedName    解决Json名字段不同
  • @Since/@Until    控制版本。在实例化Gson中设置版本。若大于等于since版本和小于until版本,显示。
  • @JsonAdapter    自定义序列化和反序列化。可以---点击打开链接

3.基础

  1.    Json数组与字符串数组
        Gson gson = new Gson();
        String jsonArray = "[\"https://github.com/xyzamo\",\"https://blog.csdn.net/sinat_24037895\",\"Java\",\"Kotlin\",\"Git\",\"GitHub\"]";
        String[] strings = gson.fromJson(jsonArray, String[].class);
        syop("Json数组转化为字符串数组"+"\n");//等同于System.out.println()
        for (String string : strings) {
            syop(string);
        }
        syop("\n字符串数组转为Json数组:");
        jsonArray = gson.toJson(jsonArray, new TypeToken<String>(){}.getType());
        syop(jsonArray);

2.Json数组与List

        Gson gson = new Gson();
        String jsonArray =  "[\"https://github.com/leavesC\",\"https://www.jianshu.com/u/9df45b87cfdf\",\"Java\",\"Kotlin\",\"Git\",\"GitHub\"]";
        List<String> stringList = gson.fromJson(jsonArray,new TypeToken<List<String>>(){}.getType());
        syop("\nJson数组转为List");
        for (String string : stringList) {
            syop(string);
        }
        //List转化为Json数组
        jsonArray = gson.toJson(stringList,new TypeToken<List<String>>(){}.getType());
        syop("\nList转化为Json数组:");
        syop(jsonArray);

3.序列化和反序列化

{
    "name": "罗纳尔多",
    "sex": "男",
    "age": 35
}  
 public void DateFor(){
        Gson gson = new GsonBuilder()
                .setPrettyPrinting()
                .setDateFormat("yyyy-mm-dd HH:mm:ss:sss")
                .create();//setPrettyPrinting格式化Json(美观)
        Date date = new Date();
        DateSimple dateSimple = new DateSimple(date,new Date(date.getTime()+10000));
        syop(dateSimple);
        syop("GSON" + gson.toJson(dateSimple));
        String json = "{\n" +
                "  \"date\": \"2018-03-17 19:38:50:033\",\n" +
                "  \"date2\": \"2018-03-17 19:55:30:033\"\n" +
                "}";
        Gson gson1 = new Gson();
        System.out.println();
        System.out.println(gson.fromJson(json,DateSimple.class));
        syop("正版数据"+gson.fromJson(gson.toJson(dateSimple),DateSimple.class));
    }
    public void CeLv(){//自定义排除策略和注解@Expose功能相似。SetExclusionStrategies()
        Gson gson = new GsonBuilder()
                .serializeNulls()
                .setPrettyPrinting()
                .setExclusionStrategies(new ExclusionStrategy() {
            @Override
            public boolean shouldSkipField(FieldAttributes f) {
                //排除指定字段
                return false;
                //return f.getName().equals("name");//排除字段为name
            }

            @Override
            public boolean shouldSkipClass(Class<?> clazz) {
                //排除字段类型
                //return clazz.getName().equals(int.class.getName());//排除字段类型为int的
                return false;

            }
        }).create();
        UserInfo userInfo = new UserInfo(null,24,"男");
        syop(gson.toJson(userInfo));



个人理解,欢迎指出错误。

猜你喜欢

转载自blog.csdn.net/sinat_24037895/article/details/80693061