首页 Java Java 使用Gson(com.google.code.gson)解析处理Json数据的方法及示例代码

Java 使用Gson(com.google.code.gson)解析处理Json数据的方法及示例代码

1、Gson的安装引用

下载地址https://github.com/google/gson/releases

1)通过Gradle引用

dependencies {
implementation 'com.google.code.gson:gson:2.8.5'
}

2)通过Maven引用

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>

2、Gson使用示例代码

1)基本数据类型示例

// 序列化
Gson gson = new Gson();
gson.toJson(1);            // ==> 1
gson.toJson("abcd");       // ==> "abcd"
gson.toJson(new Long(10)); // ==> 10
int[] values = { 1 };
gson.toJson(values);       // ==> [1]
// 反序列化
int one = gson.fromJson("1", int.hljs-string">"1", Integer.hljs-string">"1", Long.>false = gson.fromJson("false", Boolean.hljs-string">"\"abc\"", String.hljs-string">"[\"abc\"]", String[]. data-title="在线运行" onclick="runCode(2)">

2)对象类型示例

>BagOfPrimitives {
  private int value1 = 1;
  private String value2 = "abc";
  private transient int value3 = 3;
  BagOfPrimitives() {
    // no-args constructor
  }
}
// 序列化
BagOfPrimitives obj = new BagOfPrimitives();
Gson gson = new Gson();
String json = gson.toJson(obj);  
// ==> json is {"value1":1,"value2":"abc"}

注意:不能使用循环引用序列化对象,因为这会导致无限递归。

// 反序列化
BagOfPrimitives obj2 = gson.fromJson(json, BagOfPrimitives.>// ==> obj2 is just like obj

3)嵌套类(包括内部类)

Gson可以很容易地序列化静态嵌套类。
Gson还可以反序列化静态嵌套类。但是,Gson 不能自动反序列化纯内部类,
因为它们的no-args构造函数还需要对反序列化时不可用的包含Object的引用。

您可以通过使内部类静态或为其提供自定义InstanceCreator来解决此问题。示例代码:

public >A { 
  public String a; 
  >B { 
    public String b; 
    public B() {
      // No args constructor for B
    }
  } 
}

注意:上述>public >InstanceCreatorForB implements InstanceCreator<A.B> { private final A a; public InstanceCreatorForB(A a) { this.a = a; } public A.B createInstance(Type type) { return a.new B(); } }

以上是可能的,但不推荐

4)数组示例

Gson gson = new Gson();
int[] ints = {1, 2, 3, 4, 5};
String[] strings = {"abc", "def", "ghi"};
// 序列化
gson.toJson(ints);     // ==> [1,2,3,4,5]
gson.toJson(strings);  // ==> ["abc", "def", "ghi"]
// 反序列化
int[] ints2 = gson.fromJson("[1,2,3,4,5]", int[].>// ==> ints2 will be same as ints

支持具有任意复杂元素类型的多维数组。

5)Collections示例

Gson gson = new Gson();
Collection<Integer> ints = Lists.immutableList(1,2,3,4,5);
// 序列化
String json = gson.toJson(ints);  // ==> json is [1,2,3,4,5]
// 反序列化
Type collectionType = new TypeToken<Collection<Integer>>(){}.getType();
Collection<Integer> ints2 = gson.fromJson(json, collectionType);
// ==> ints2 is same as ints

6)序列化和反序列化泛型类型示例

>Foo<T> {
  T value;
}
Gson gson = new Gson();
Foo<Bar> foo = new Foo<Bar>();
gson.toJson(foo); // May not serialize foo.value correctly
gson.fromJson(json, foo.getClass()); // Fails to deserialize foo.value as Bar

上面的代码无法将值解释为类型Bar,因为Gson调用list.getClass()获取其类信息,但此方法返回一个原始类,Foo.>Type fooType = new TypeToken<Foo<Bar>>() {}.getType();
gson.toJson(foo, fooType);
gson.fromJson(json, fooType);

参考文档https://github.com/google/gson/blob/master/UserGuide.md

特别声明:本站部分内容收集于互联网是出于更直观传递信息的目的。该内容版权归原作者所有,并不代表本站赞同其观点和对其真实性负责。如该内容涉及任何第三方合法权利,请及时与824310991@qq.com联系,我们会及时反馈并处理完毕。