如何在 Java 中使用 GSON 或其他 JSON 库反序列化列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4318458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:48:53  来源:igfitidea点击:

How to deserialize a list using GSON or another JSON library in Java?

javajsongson

提问by Valter Silva

I can serialize a List<Video>in my servlet on GAE, but I can't deserialize it. What am I doing wrong?

我可以List<Video>在 GAE 上的 servlet 中序列化 a ,但无法反序列化它。我究竟做错了什么?

This is my class Video in GAE, which is serialized:

这是我在GAE中的类Video,序列化了:

package legiontube;

import java.util.Date;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Video {

    @PrimaryKey
    private String id;

    @Persistent
    private String titulo;

    @Persistent
    private String descricao;

    @Persistent
    private Date date;

    public Video(){};

 public Video(String id, String titulo, String descricao, Date date) {
  //super();
  this.id = id;
  this.titulo = titulo;
  this.descricao = descricao;
  this.date = date;
 }

 public String getId() {
  return id;
 }

 public void setId(String id) {
  this.id = id;
 }

 public String getTitulo() {
  return titulo;
 }

 public void setTitulo(String titulo) {
  this.titulo = titulo;
 }

 public String getDescricao() {
  return descricao;
 }

 public void setDescricao(String descricao) {
  this.descricao = descricao;
 }

 public Date getDate() {
  return date;
 }

 public void setDate(Date date) {
  this.date = date;
 }

}

This is my class Video in my other application, where I try to deserialize:

这是我在其他应用程序中的类 Video,我尝试反序列化:

package classes;

import java.util.Date;

public class Video {
 private String id;
 private String titulo;
 private String descricao;
 private Date date;

 public Video(String id, String titulo, String descricao, Date date) {
  //super();
  this.id = id;
  this.titulo = titulo;
  this.descricao = descricao;
  this.date = date;
 }

 public String getId() {
  return id;
 }
 public void setId(String id) {
  this.id = id;
 }
 public String getTitulo() {
  return titulo;
 }
 public void setTitulo(String titulo) {
  this.titulo = titulo;
 }
 public String getDescricao() {
  return descricao;
 }
 public void setDescricao(String descricao) {
  this.descricao = descricao;
 }
 public Date getDate() {
  return date;
 }
 public void setDate(Date date) {
  this.date = date;
 }

}

采纳答案by ColinD

With Gson, you'd just need to do something like:

使用 Gson,您只需要执行以下操作:

List<Video> videos = gson.fromJson(json, new TypeToken<List<Video>>(){}.getType());

You might also need to provide a no-arg constructor on the Videoclass you're deserializing to.

您可能还需要在Video要反序列化的类上提供无参数构造函数。

回答by DevNG

Another way is to use an array as a type, e.g.:

另一种方法是使用数组作为类型,例如:

Video[] videoArray = gson.fromJson(json, Video[].class);

This way you avoid all the hassle with the Type object, and if you really need a list you can always convert the array to a list, e.g.:

这样你就可以避免使用 Type 对象的所有麻烦,如果你真的需要一个列表,你可以随时将数组转换为列表,例如:

List<Video> videoList = Arrays.asList(videoArray);

IMHO this is much more readable.

恕我直言,这更具可读性。



In Kotlinthis looks like this:

Kotlin 中,这看起来像这样:

Gson().fromJson(jsonString, Array<Video>::class.java)

To convert this array into List, just use .toList()method

将此数组转换为列表,只需使用.toList()方法

回答by Bogdan Kornev

Be careful using the answer provide by @DevNG. Arrays.asList() returns internal implementation of ArrayList that doesn't implement some useful methods like add(), delete(), etc. If you call them an UnsupportedOperationException will be thrown. In order to get real ArrayList instance you need to write something like this:

小心使用@DevNG 提供的答案。Arrays.asList() 返回 ArrayList 的内部实现,它没有实现一些有用的方法,如 add()、delete() 等。如果你调用它们,将抛出 UnsupportedOperationException。为了获得真正的 ArrayList 实例,您需要编写如下内容:

List<Video> = new ArrayList<>(Arrays.asList(videoArray));

回答by naXa

Try this one-liner

试试这个单线

List<Video> videos = Arrays.asList(new Gson().fromJson(json, Video[].class));

Warning: the list of videos, returned by Arrays.asListis immutable - you can't insert new values.

警告:videos, 返回的列表Arrays.asList是不可变的 - 您不能插入新值。



Reference:

参考:

  1. Method Arrays#asList
  2. Constructor Gson
  3. Method Gson#fromJson(source jsonmay be of type JsonElement, Reader, or String)
  4. Interface List
  5. JLS - Arrays
  6. JLS - Generic Interfaces
  1. 方法数组#asList
  2. 构造函数Gson
  3. 方法GSON#fromJson(源json可以是类型的JsonElementReaderString
  4. 接口列表
  5. JLS - 数组
  6. JLS - 通用接口