首页 文章

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

提问于
浏览 508 次
97

我可以在GAE上序列化我的servlet中的List,但我不能反序列化它 . 我究竟做错了什么?

这是我在GAE中的视频类,它是序列化的:

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;

 /**
  * @param id
  * @param titulo
  * @param descricao
  * @param 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;
 }

}

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

package classes;

import java.util.Date;

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

 /**
  * @param id
  * @param titulo
  * @param descricao
  * @param 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;
 }

}

5 回答

  • 272

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

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

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

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

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

  • 8

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

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

    有了Gson,你只需要做以下事情:

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

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

  • 2

    试试这个单线

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

    参考:

  • 0

    Jackson JSON Java Parser非常受欢迎,并在Spring框架中使用

    import java.io.IOException;
    import java.io.StringWriter;
    import java.util.List;
    
    import com.fasterxml.jackson.core.JsonGenerationException;
    import com.fasterxml.jackson.core.JsonParseException;
    import com.fasterxml.jackson.core.type.TypeReference;
    import com.fasterxml.jackson.databind.DeserializationFeature;
    import com.fasterxml.jackson.databind.JsonMappingException;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.fasterxml.jackson.databind.SerializationFeature;
    
    public static <T> T jsonStringToObjectArray(String jsonString, Class<T> clazz) throws JsonParseException, JsonMappingException, IOException {
            T obj = null;
            ObjectMapper mapper = new ObjectMapper();
            mapper = new ObjectMapper().configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
            obj = mapper.readValue(jsonString, mapper.getTypeFactory().constructCollectionType(List.class, clazz));
            return obj;
        }
    

相关问题