Gson - Convert from JSON to a Typed Arraylist<T>

Gson - convert from Json to a typed ArrayList T

You may use TypeToken to load the json string into a custom object.

logs = gson.fromJson(br, new TypeToken<List<JsonLog>>(){}.getType());

Documentation:

Represents a generic type T.

Java doesn't yet provide a way to represent generic types, so this class does. Forces clients to create a subclass of this class which enables retrieval the type information even at runtime.

For example, to create a type literal for List<String>, you can create an empty anonymous inner class:

TypeToken<List<String>> list = new TypeToken<List<String>>() {};

This syntax cannot be used to create type literals that have wildcard parameters, such as Class<?> or List<? extends CharSequence>.

Kotlin:

If you need to do it in Kotlin you can do it like this:

val myType = object : TypeToken<List<JsonLong>>() {}.type
val logs = gson.fromJson<List<JsonLong>>(br, myType)

Or you can see this answer for various alternatives.

How to use gson to convert json to arraylist if the list contain different class?

you can use the below code to convert json to corresponding list of objects

TypeToken<List<Animal>> token = new TypeToken<List<Animal>>() {};
List<Animal> animals = gson.fromJson(data, token.getType());

Converting from JSON using GSON, cannot cast ArrayList into object

I believe the issue is that the Playlist class extends ArrayList. GSON is trying to serialize your Playlist as a list of Parcelable. Judging by your class, it doesn't look like Playlist truly needs to extend ArrayList. I tried running your example (I had to remove a lot in order to get down to the bare minimum) and it was serializing each Playlist as an empty list (the string "[]"), regardless of what was in playlistSongs. I removed the superclass and it started working as expected.

Converting a JSON String To an ArrayList of Objects with GSON

First make a POJO class:

public class UserDataRecord {
public int G_ID1;
public int G_ID2;
public int G_ID3;
public String NAME;
public String getNAME() {
return NAME;
}
}

Next use gson to deserialize your json string like so:

UserDataRecord[] userDataRecords = gson.fromJson(jsonUserDataRecs, UserDataRecord[].class);

Now you have an array of the deserialized user data records. You can manually convert this to a list if you need to, or just iterate it using a normal for loop.

Deserialize a List T object with Gson?

Method to deserialize generic collection:

import java.lang.reflect.Type;
import com.google.gson.reflect.TypeToken;

...

Type listType = new TypeToken<ArrayList<YourClass>>(){}.getType();
List<YourClass> yourClassList = new Gson().fromJson(jsonArray, listType);

Since several people in the comments have mentioned it, here's an explanation of how the TypeToken class is being used. The construction new TypeToken<...>() {}.getType() captures a compile-time type (between the < and >) into a runtime java.lang.reflect.Type object. Unlike a Class object, which can only represent a raw (erased) type, the Type object can represent any type in the Java language, including a parameterized instantiation of a generic type.

The TypeToken class itself does not have a public constructor, because you're not supposed to construct it directly. Instead, you always construct an anonymous subclass (hence the {}, which is a necessary part of this expression).

Due to type erasure, the TypeToken class is only able to capture types that are fully known at compile time. (That is, you can't do new TypeToken<List<T>>() {}.getType() for a type parameter T.)

For more information, see the documentation for the TypeToken class.



Related Topics



Leave a reply



Submit