gson

Gson - parsing json with field that is array or string

大兔子大兔子 提交于 2019-12-22 09:56:03
问题 I'm trying to parse some json with Gson and I have following problem: This is my json: [{ "label": "Check Digit", "value": "W" }, { "label": "Equipment", "value": [ "With Standard Liftgate", "(-) Version Packages" ] }] This is my java class: public class Decode { private String label; private List<String> value = new ArrayList<>(); public String getLabel() { return label; } public void setLabel(String label) { this.label = label; } public List<String> getValue() { return value; } public void

How to post data using HashMap in retrofit?

与世无争的帅哥 提交于 2019-12-22 08:37:12
问题 Can you please explain how to post data using hashmap in retrofit2 ? 回答1: This is what I post @FormUrlEncoded @POST("getProfile") Call<YourResponseObject> getProfile(@FieldMap HashMap<String, String> data); And the HashMap HashMap<String, String> map = new HashMap<>(); map.put("token", "yourtoken"); map.put("yourvariable", "yourvariable"); 回答2: From Retrofit2 documentation check FieldMap for more details You need to create your interface public interface YourPostService { @FormUrlEncoded

Deserializing an object that contains JSON using GSON

浪尽此生 提交于 2019-12-22 07:55:11
问题 I'm using gson to deserialize POJO objects from JSON representations. I'd like one of the fields in one of my POJOs to contain arbitrary JSON data. For example: class B { public String stringField; public JsonObject jsonField; } I'd like to be able to call Gson.fromJson(json, B.class) on the following JSON: { "stringField": "booger", "jsonField" : { "arbitraryField1": "foo" } } and have the resulting B.jsonField contain a JsonObject with an arbitraryField of value foo . However, when I

Can't cast JsonNull to JsonObject

大城市里の小女人 提交于 2019-12-22 07:09:08
问题 I want to copy one primitve property from one JsonObject to another JsonObject propertyToBeCopied = source.getAsJsonObject(propertyName); but I always run into this exception: com.google.gson.JsonNull cannot be cast to com.google.gson.JsonObject According to the documentation it should be possible to do the cast, or am I wrong? 回答1: According to the docs JsonNull is a JsonElement but not a JsonObject (which is itself a JsonElement ). Using JsonElement element = source.get(propertyName); if (!

Gson deserialization for Realm list of primitives

别说谁变了你拦得住时间么 提交于 2019-12-22 06:49:05
问题 I am using realm with gson. I have a modal which has a list of int type field. Realm does not support currently list of primitives. To solve this there is a solution. I created my RealmInt class. import io.realm.RealmObject; public class RealmInt extends RealmObject { private int val; public int getVal() { return val; } public void setVal(int val) { this.val = val; } } I have a big modal object something like that.. public class Product extends RealmObject { @PrimaryKey private int productID;

How to convert a Date between Jackson and Gson?

南楼画角 提交于 2019-12-22 06:33:44
问题 In our Spring-configured REST-server we use Jackson to convert an object into Json. This object contains several java.util.Date objects. When we attempt to deserialize it on an Android device using Gson's fromJson method we get a "java.text.ParseException: Unparseable date". We have tried serializing the date to a timestamp corresponding to milliseconds since 1970, but get the same exception. Can Gson be configured to parse a timestamp-formatted date such as 1291158000000 into a java.util

Unable to parse error in Retrofit 2

喜你入骨 提交于 2019-12-22 05:26:07
问题 I am using Retrofit 2 and I am getting a null pointer exception at this line: RetrofitClient.APIError error = RetrofitClient.ErrorUtils.parseError(response, retrofit); The error is null. More details: This is the format that the API returns the error in: { "error": { "message": "Incorrect credentials", "statusCode": 401 } } Here is my login Callback code: new Callback<LoginResponse>() { @Override public void onResponse(Response<LoginResponse> response, Retrofit retrofit) { if (listener !=

RoboSpice with Gson and Realm

北城余情 提交于 2019-12-22 05:17:11
问题 I'm using RoboSpice with GsonSpringAndroidSpiceService. I also want to add Realm to save the data. the problem is that in realm each object has to extend realmObject, but the gson in the roboSpice trying to parse the realmObject instead of ignore it. I've tried to add exclusion stategy: Gson gson = new GsonBuilder() .setExclusionStrategies(new ExclusionStrategy() { @Override public boolean shouldSkipField(FieldAttributes f) { return f.getDeclaringClass().equals(RealmObject.class); } @Override

GSON: .isJsonNull() question

守給你的承諾、 提交于 2019-12-22 04:43:18
问题 I am reading in a JSON file (using Google's GSON ). One of my tests checks program's behavior in event file a given key is missing. JsonElement value = e.getAsJsonObject().get(ENVIRONMENT); My expectation is that when .get(ing) this key, i would get null . Turns out i do. When i .get(ENVIRONMENT) , value returned is null . When i test it, i actually get a " not null ". Weird, considering, GSON's javadoc says " provides check for verifying if this element represents a null value or not " if

Kotlin convert json array to model list using GSON

こ雲淡風輕ζ 提交于 2019-12-22 04:28:07
问题 I am not able to convert a JSON Array into a GroupModel array. Below is the JSON I used: [{ "description":"My expense to others", "items":["aaa","bbb"], "name":"My Expense" }, { "description":"My expense to others"," items":["aaa","bbb"], "name":"My Expense" }] and the GroupModel class is: class GroupModel { var name: String? = null var description: String? = null var items: MutableList<String>? = null constructor(name: String, description: String, items: MutableList<String>) { this.name =