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

I can serialize the List<Video> in my servlet to GAE, but I cannot deserialize it. What am I doing wrong?

This is my GAE Video class, which is serialized:

 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 a video of my class in another application where I am trying to deserialize:

 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; } } 
+103
java json gson
Nov 30 '10 at 20:49
source share
5 answers

With Gson, you just need to do something like:

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

You may also need to create a no-arg constructor in the Video class that you are deserializing.

+290
Nov 30 '10 at 21:14
source share

Another way is to use the array as a type, for example:

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

This way you avoid all the problems with the Type object, and if you really need a list, you can always convert the array to a list, for example:

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

IMHO it is much readable.

+95
Jun 25 '13 at 14:21
source share

Try this one line

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

Warning: the list of videos returned by Arrays.asList is unchanged - you cannot insert new values.




Link:

  1. Arrays of methods # asList
  2. Gson Constructor
  3. Gson # fromJson method (source json can be of type JsonElement , Reader or String )
  4. Interface List
  5. JLS - Arrays
  6. JLS - Universal Interfaces
+7
Oct 18 '16 at 10:37
source share

Be careful using the answer provided by @DevNG. Arrays.asList () returns an internal implementation of ArrayList that does not implement some useful methods such as add (), delete (), etc. If you call them, a UnsupportedOperationException will be thrown. To get a real instance of ArrayList, you need to write something like this:

 List<Video> = new ArrayList<>(Arrays.asList(videoArray)); 
+2
May 23 '16 at 9:02
source share

Jackson JSON Java Parser is very popular and used in the Spring environment

 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; } 
0
Apr 16 '18 at 11:30
source share



All Articles