gson

Why can't I fetch nested data from json using Gson?

£可爱£侵袭症+ 提交于 2021-01-29 07:11:58
问题 I have a json I fetch from an API that looks like this: {"success":true,"message":"","result":{"Bid":6886.97100000,"Ask":6891.58500000,"Last":6891.58500000}} All I want is to just save Bid and Ask values in fields of a class. First I parse whole json like this: val response = sendRequest(url)) val gson = Gson() val ticker : MarketTickerEntity = gson.fromJson(response, MarketTickerEntity::class.java) And then I try to parse it inside of my class init block and reassign to bid and ask fields.

java.lang.NoSuchMethodError: com.google.gson.GsonBuilder.setLenient()Lcom/google/gson/GsonBuilder;

拟墨画扇 提交于 2021-01-29 05:30:28
问题 When I try to create a RemoteWebDriver in 3.11, I see this error below. This worked fine up through 3.10. I've reverted to 3.10 for now. I tried adding in the gson jar from google, but I still see same error. Anyone else see this? I'm running one selenium server as hub, and then running a node against it with webdriver for ff or chrome, same issue. com.google.gson.GsonBuilder.setLenient()Lcom/google/gson/GsonBuilder; java.lang.NoSuchMethodError: com.google.gson.GsonBuilder.setLenient()Lcom

How to get the data from the JSON list of JSON list

久未见 提交于 2021-01-29 05:24:59
问题 I've got an JSON string from my API, looks like this: [ { "id": "abc", "data": { "Name": "Peter", "Date": "2017/12/01" } }, { "id": "def", "data": { "Name": "Tina", "Date": "2017/12/20" } }, { "id": "ghi", "data": { "Name": "Amy", "Date": "2017/12/16" } } ] Then, I use (java): Gson gson = new Gson(); Type resultType = new TypeToken<List<Map<String, Object>>>() { }.getType(); List<Map<String, Object>> result = gson.fromJson(info, resultType); if I call result.get(0).toString()); then it

Parse Json to Primative Array Kotlin

六月ゝ 毕业季﹏ 提交于 2021-01-28 12:32:00
问题 Maybe this is simple, but I'm missing how to do this. I'm using GSON, kotlin, and retrofit Data.json { "array1":[1,2], "array2":[1,2] } DataObject.kt data class DataObject(array1: List<Int>, array2: List<Int>) The the above fails to deserialize the arrays. 回答1: This is a running example: data class DataObject(val array1: List<Int>, val array2: List<Int>) fun main(args: Array<String>) { val json = """{"array1":[1,2],"array2":[1,2]}""" println(Gson().fromJson(json, DataObject::class.java)) /

GSON can be an array of string or an array of object

試著忘記壹切 提交于 2021-01-28 12:04:27
问题 I'm trying to create a GSON class but not sure how to handle this case. According to the API specifications options can be a list values: ["one", "two"] OR can be a list of {"value": "Label"} pairs to provide labels for values { ... "options": ["one", "two", "three"], } OR { ... "options": [{"light": "Solarized light"}, {"dark": "Solarized dark"}], } 回答1: You can map this field to Map<String, String> : class Pojo { @JsonAdapter(OptionsJsonDeserializer.class) private Map<String, String>

What is the role of 'com.google.code.gson:gson:2.6.1' dependency in retrofit 2 when it has its own gson converter?

僤鯓⒐⒋嵵緔 提交于 2021-01-28 09:12:05
问题 // Retrofit compile 'com.squareup.retrofit2:retrofit:2.1.0' // JSON Parsing compile 'com.google.code.gson:gson:2.6.1' compile 'com.squareup.retrofit2:converter-gson:2.1.0' My question is regarding the second and third dependency added. I do understand it is related to JSON conversion. If the third dependency is added,would the second dependency still be needed? I have seen them both being added on several examples. For e.g: https://www.androidhive.info/2016/05/android-working-with-retrofit

How correctly use Gson to deserialize an object of generic type through a static utility method?

与世无争的帅哥 提交于 2021-01-28 08:57:29
问题 I've seen a number of other similar looking questions, but I think there's a level of abstraction on top of those that makes the difference. Namely, I have a utility class with a static generic wrapper method to deserialize an object of generic type (unknown at build time): public final class Utils { public static final Gson sGson = new Gson(); public static synchronized <T> T fromJson(String doc) { return sGson.fromJson(doc, new TypeToken<T>(){}.getType()); } } A simple class to test it on:

Jackson/Gson serialize and deserialize JavaFX Properties to json

两盒软妹~` 提交于 2021-01-28 05:30:29
问题 I have added a BooleanProperty into a DAO class which is to be serialized into JSON and sent across to a server to be saved in a MySQL database. The reason I am using the BooleanProperty is because I want to use data binding for the 'isActive' field in my JavaFX desktop application. The class to be serialized: package com.example.myapplication import lombok.Data; import javafx.beans.property.BooleanProperty; import javafx.beans.property.SimpleBooleanProperty; @Data public class MyDAO {

Gson don't use default field value for objects (not primitives)

本小妞迷上赌 提交于 2021-01-27 19:27:34
问题 I use gson in android project and got problem. I want to set default value for list object if Api give me null value for this field. I don't modify this field any more so i don't want to check it every time for null value So if be more detailed I have for example this class class Model { @Expose private int test = 1; @Expose private int primitive = 1; @Expose private List<String> strings = new ArrayList<>(); } and json from Api { "test" : 12 } so i expect what gson will generate object with

Retrofit2: convert JSON with dynamic keys to a Map<String, Model> with Model also containing those keys

北城余情 提交于 2021-01-27 18:52:37
问题 I'm using Retrofit 2 in combination with Gson and RxJava. My JSON data looks something like this: { "groups": { "1": { "name": "First group", "type": "some data", // ... more data }, "2": { "name": "Second group", "type": "some data", // ... more data }, "3": { "name": "Third group", "type": "some data", // ... more data } // 4, 5, 6, etc... }, // ... more data } In the example above the "keys" 1, 2, 3 are integers but they can also be unique strings. I want to map this JSON data to something