InputStream to JsonObject – GSON
In your JsonResult class change public class JsonResult { private String status; private Meta meta; @SerializedName(“data”) private ArrayList<Player> players; }
In your JsonResult class change public class JsonResult { private String status; private Meta meta; @SerializedName(“data”) private ArrayList<Player> players; }
The accepted answer did not work for me, but this did: Download the GSON JAR file and copy it to your /libs/ folder inside your application project. Open the build.gradle file at the root level of your project and edit your dependencies to include the new .jar file: dependencies { compile fileTree(dir: ‘libs’, include: ‘*.jar’) … Read more
The most straightforward approach I can think of is to just treat the structure as a Map (of Map). With Gson, this is relatively easy to do, as long as the Map structure is statically known, every branch from the root has the same depth, and everything is a String. import java.io.FileReader; import java.lang.reflect.Type; import … Read more
You can’t use generics with class, as easily observable here: List<Int>::class.java It gives you the same error. To use the generic typ in GSON deserialization, do what is suggested here: https://stackoverflow.com/a/5554296/8073652 EDIT: In Kotlin it looks like this: val type: Type = object : TypeToken<ServiceCall<SurveyListModel>>() {}.type Gson().fromJson<ServiceCall<SurveyListModel>>(json, type).result Here’s a small proof of concept, I’ve … Read more
If you see the answer there, you can notice that the first parameter in the fromJson() method was a String(the json object). Try to use the toString() on the JsonArray like this:- List<MyModel> myModelList = gson.fromJson(jsonArray.toString(), listType);
When parsing such a simple structure, no need to have dedicated classes. Solution 1 : To get the imgurURL from your String with gson, you can do this : JsonParser parser = new JsonParser(); JsonObject obj = parser.parse(toExtract).getAsJsonObject(); String imgurl = obj.get(“imgurl”).getAsString(); This uses a raw parsing into a JsonObject. Solution 2 : Alternatively, you … Read more
enum class VehicleEnumEntity(val value: String) { @SerializedName(“vehicle”) CAR(“vehicle”), @SerializedName(“motorcycle”) MOTORCYCLE(“motorcycle”), @SerializedName(“van”) VAN(“van”), @SerializedName(“motorhome”) MOTORHOME(“motorhome”), @SerializedName(“other”) OTHER(“other”) } Source
A simpler way I found (just now) to do this is to use the @SerializedName annotation. I found it in the EnumTest.java here (the Gender class around ln 195): https://code.google.com/p/google-gson/source/browse/trunk/gson/src/test/java/com/google/gson/functional/EnumTest.java?r=1230 This assumes that all of your Types will come in as lowercase as opposed to being “case insensitive” public enum Type { @SerializedName(“live”) LIVE, @SerializedName(“upcoming”) … Read more
I had the same issue, with version 2.3.1 of Gson. I got it working by using .registerTypeHierarchyAdapter(Player.class, new PlayerTypeAdapter()) instead of .registerTypeAdapter(Player.class, new PlayerTypeAdapter())
(After OP commented that in fact the JSON looks like this, I completely updated the answer.) Solution for Gson 2.0+ I just learned that with newer Gson versions this is extremely simple: GsonBuilder builder = new GsonBuilder(); Object o = builder.create().fromJson(json, Object.class); The created object is a Map (com.google.gson.internal.LinkedTreeMap), and if you print it, it … Read more