Kotlin convert json string to list of object using Gson

K.Os

I have a problem with Kotlin to write code for conversion from JSON String to List of objects.

Normally in Java, it is like this:

  Gson gson = new Gson();
    Type type = new TypeToken<List<SomeOjbect>>() {}.getType();
    List<SomeOjbect> measurements = gson.fromJson(json, type);
    return measurements;

However in Kotlin, when i try it like this:

   val gson = Gson()
    val type: Type = TypeToken<List<SomeOjbect>>{}.type
    val measurements : List<SomeOjbect> = gson.fromJson(text, type)
    return measurements 

The IDE Android Studio underlines as error the TypeToken saying:

Cannot access ' < init > ': it is public/package/ in 'TypeToken'

and also underlines as error the {} saying:

Type mismatch. Required: Type! Found: () → Unit

So is there a solution to make it work for Kotlin?

Charlie Niekirk

You could try doing this instead:

val objectList = gson.fromJson(json, Array<SomeObject>::class.java).asList()

EDIT [14th January 2020]: You should NOT be using GSON anymore. Jake Wharton, one of the projects maintainers, suggests using Moshi, Jackson or kotlinx.serialization.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Convert JSONArray to List<HashMap<String,Object>> in java without GSON

GSON - convert JsonObject to java/Kotlin object using a map

How to convert json timezone object to java TimeZone using Gson?

How to convert Json to Java object using Gson

Convert Java object list to correct name value pair format in Json using Gson

JSON String to Java object using GSON

Convert from JSon to multiple unknown java object types using GSon

How to convert string array to object using GSON/ JSON?

How to convert list of Json string to JsonObject using Gson

How to convert List to a JSON Object using GSON?

Gson - Convert from json string to list of objects

Kotlin: get List object from JSON string

Convert Anonymous java object types to JSON using GSon

Kotlin convert json array to model list using GSON

How can I convert a JSON string of objects into an ArrayList using Gson?

How to insert Json Object inside Json Array in Kotlin using gson

Convert data class (JSON object) to an list in kotlin

Convert to list of data class object from JSON string using Gson?

Convert a (String, List[(String, String)]) to JSON object

Kotlin: convert nested JSON object to literal string

Convert JSON data to Java object (including the Object class) using GSON

Convert JSON to Java object second parameter null using Gson library

Parse list object json using Gson

How do I convert a JSON String into a GSON Object?

convert json to json object using gson

Parse list of json string using GSON

How to convert a JSON map into a custom Java list, using Gson?

Constructing Kotlin classes for using Gson to convert json response to class

Error when trying to convert Java object to JSON string using GSON