zoukankan      html  css  js  c++  java
  • gson使用

    gson 作用: json <-> java bean

    1. 基本类型

    (Serialization)
    Gson gson = new Gson();

    gson.toJson(1);            ==> prints 1
    gson.toJson("abcd");       ==> prints "abcd"
    gson.toJson(new Long(10)); ==> prints 10
    int[] values = { 1 };
    gson.toJson(values);       ==> prints [1]

    (Deserialization)
    int one = 
    gson.fromJson("1", int.class);
    Integer one = gson.fromJson("1", Integer.class);
    Long one = gson.fromJson("1", Long.class);
    Boolean false = 
    gson.fromJson("false", Boolean.class);
    String str = 
    gson.fromJson("\"abc\""String.class);
    String anotherStr = gson.fromJson("[\"abc\"]"String.class);

    2.object

    class BagOfPrimitives {
      private int value1 = 1;
      private String value2 = "abc";
      private transient int value3 = 3;
      BagOfPrimitives() {
        // no-args constructor
      }
    }

    (Serialization)
    BagOfPrimitives obj = new BagOfPrimitives();
    Gson gson = new Gson();
    String json = gson.toJson(obj);  
    ==> json is {"value1":1,"value2":"abc"}

    (Deserialization)
    BagOfPrimitives obj2 = 
    gson.fromJson(json, BagOfPrimitives.class);   
    ==> obj2 is just like obj

    3.数组

    Gson gson = new Gson();
    int[] ints = {1, 2, 3, 4, 5};
    String[] strings = {"abc", "def", "ghi"};

    (Serialization)
    gson.toJson(ints);     ==> prints [1,2,3,4,5]

    gson.toJson(strings);  ==> prints ["abc", "def", "ghi"]

    (Deserialization)
    int[] ints2 = 
    gson.fromJson("[1,2,3,4,5]", int[].class); 
    ==> ints2 will be same as ints

    4.集合

    Gson gson = new Gson();
    Collection<Integer> ints = Lists.immutableList(1,2,3,4,5);

    (Serialization)
    String json = 
    gson.toJson(ints); ==> json is [1,2,3,4,5]

    (Deserialization)

    Type collectionType = new TypeToken<Collection<Integer>>(){}.getType();
    Collection<Integer> ints2 = 
    gson.fromJson(json, collectionType);
    ints2 is same as ints

    5. gson 常见创建参数

    Gson gson = new GsonBuilder().create()

    Gson gson = new GsonBuilder().setPrettyPrinting().create(); ->  use the default JsonPrintFormatter instead of the JsonCompactFormatter

    Gson gson = new GsonBuilder().serializeNulls().create(); -> 序列化 Null

    Gson gson = new GsonBuilder().setVersion(1.0).create(); @Since -> 版本支持

    Gson gson = new GsonBuilder() .excludeFieldsWithModifier(Modifier.STATIC, Modifier.TRANSIENT, Modifier.VOLATILE) .create();  

    GsonBuilder().excludeFieldsWithoutExposeAnnotation().create()  @Expose  ->  去掉字段

    JSON  key 与 bean name对应

    private class SomeObject {
      @SerializedName("custom_naming") private final String someField;
      private final String someOtherField;

      public SomeObject(String a, String b) {
        this.someField = a;
        this.someOtherField = b;
      }
    }

    SomeObject someObject = new SomeObject("first", "second");
    Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).create();
    String jsonRepresentation = gson.toJson(someObject);
    System.out.println(jsonRepresentation);

    ======== OUTPUT ========
    {"custom_naming":"first","SomeOtherField":"second"}

  • 相关阅读:
    实现Runnable接口和继承Thread类的区别
    图的DFS和BFS
    图建模
    数据结构-图的基本知识和表示
    除自身以外的乘积数组(力扣第238题)
    MapReduce源码分析--Shuffle阶段
    转到博客园
    vue中使用剪切板插件 clipboard.js
    vue中使用vue-qrcode生成二维码
    h5中嵌入视频自动播放的问题
  • 原文地址:https://www.cnblogs.com/zeng200103/p/3106395.html
Copyright © 2011-2022 走看看