gson

Serialize Query-Parameter in Retrofit

我们两清 提交于 2019-12-21 04:12:49
问题 Imagine the following request: @POST("/recipes/create") void createRecipe(@Query("recipe") Recipe recipe, Callback<String> callback); I would like to have toJson(recipe) but unfortunately my request is just calling toString() for my recipe which does not work at all. I could override the toString inside of Recipe but I'd rather have a general solution. I cannot use @Body as I need to specify, what I'm sending (i need to have "recipe=json(theRecipe)". I also cannot change the serialization to

DateFormat pattern “yyyy-MM-dd'T'HH:mm:ss.SSS'Z'” in Gson

泪湿孤枕 提交于 2019-12-21 03:48:33
问题 I have two field like below ( pay attention that the first field has milliseconds section ): { "updateTime":"2011-11-02T02:50:12.208Z", "deliverTime":"1899-12-31T16:00:00Z" } I want to deserialize the Json string to an object with Gson, so I get a Gson instance: GsonBuilder gb = new GsonBuilder(); gb.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); gson = gb.create(); The first field is deserialized to Java date type: 2011-11-02 02:50:12.208 (looks like ignored the time zone section-‘Z’, that's

Gson.toString() gives error “IllegalArgumentException: multiple JSON fields named mPaint”

◇◆丶佛笑我妖孽 提交于 2019-12-21 02:49:21
问题 I want to convert a custom object into a string and save in SharePreferences which is my ultimate goal. I tried below line which fails. String matchString = gson.toJson(userMatches); Logcat : 10-11 15:24:33.245: E/AndroidRuntime(21427): FATAL EXCEPTION: main 10-11 15:24:33.245: E/AndroidRuntime(21427): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=4001, result=-1, data=null} to activity {com.objectlounge.ridesharebuddy/com.objectlounge.ridesharebuddy

java.lang.NumberFormatException: Expected an int but was 0.6 at line 1 column 8454

只谈情不闲聊 提交于 2019-12-20 20:35:14
问题 I'm using the retrofit library for my calls in a demo project. I received the following error: java.lang.NumberFormatException: Expected an int but was 0.6 at line 1 column 8454 path $.result.results.ads[2].acres I under stand that this is down to GSON. I will show you the JSON it's getting caught in: { "ad_id":739580087654, "property_type":"site", "house_type":"", "selling_type":"private-treaty", "price_type":"", "agreed":0, "priority":2, "description":"Beautiful elevated 0.6 acre site -

Using Gson and JsonObject to format and parse data

拈花ヽ惹草 提交于 2019-12-20 20:18:13
问题 I am using JsonObject and Gson to format the data i need to send in the form of String and then retrieve and parse it somewhere else. This is my simple code which is not working: public static void main(String[] args) { Gson g = new Gson(); Gson listG = new Gson(); ArrayList<String> l= new ArrayList<String>(); l.add("abcd"); l.add("efgh"); l.add("ijkl"); String list = listG.toJson(l); JsonObject jObject = new JsonObject(); jObject.addProperty("command" , 1); jObject.addProperty("message" ,

Gradle returns package does not exists [duplicate]

送分小仙女□ 提交于 2019-12-20 17:37:49
问题 This question already has answers here : Specifying Maven dependencies in build.gradle doesn't work (5 answers) Closed 9 months ago . I'm trying to add the gson library to my android project (I'm devloping using the Andrdoid-studio). To add the library, I changed the AppProject/AppName/build.gradle file in this way: buildscript { repositories { mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:0.4' } } apply plugin: 'android' repositories { mavenCentral() }

Gradle returns package does not exists [duplicate]

牧云@^-^@ 提交于 2019-12-20 17:36:18
问题 This question already has answers here : Specifying Maven dependencies in build.gradle doesn't work (5 answers) Closed 9 months ago . I'm trying to add the gson library to my android project (I'm devloping using the Andrdoid-studio). To add the library, I changed the AppProject/AppName/build.gradle file in this way: buildscript { repositories { mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:0.4' } } apply plugin: 'android' repositories { mavenCentral() }

org.json.JSONObject vs Gson library JsonObject

左心房为你撑大大i 提交于 2019-12-20 17:24:44
问题 What are differences between this two classes? If someone uses Gson library is it preferable to use com.google.json.JsonObject over org.json.JSONObject ? Could anybody list pros and cons of these 2 choices? 回答1: Many JSON implementations are available in the market and most of them are open source. Each one has specific advantages and disadvantages. Google GSON Jackson org.json etc. Google GSON click for official documents Provide simple toJson() and fromJson() methods to convert Java objects

Gson deserializer with Retrofit converter: just need inner JSON for all responses

我与影子孤独终老i 提交于 2019-12-20 15:16:42
问题 I am working with an API that always responds like so: { "stuff_i_need": [ { "title": "Hello" }, { "title": "World!" } ], "status": "success" } and { "other_things_key": { "version": "208" }, "status": "success" } There are always two elements, and I just need the one that is not "status." I want to do this with one GsonBuilder, as well. I tried: new GsonConverter(new GsonBuilder() .registerTypeAdapter(List.class, new JsonDeserializer<List>() { @Override public List deserialize(JsonElement

[Object Object] 解决方案 JSON.stringify()

青春壹個敷衍的年華 提交于 2019-12-20 11:46:37
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> [Object Object] 说明 [object Object]是对象的字符串形式,由于隐式调用了 Object 对象的 toString() 方法,形式是:"[object Object]"。 [object Object] 表示的就只是一个对象,当前对象 toString() 没有正确解析,可以使用 JSON.stringify() 来避免这个问题。 Json.stringify() 是序列化函数,用于将对象转化为字符串;Json.parse() 是反序列化函数,用于将字符串转化为 Json 对象; 问题分析 此处是前后端分析开发项目,Vue + SpringBoot,前后端一般通过 Json 数据交互。此处"日志列表查询"后端接收到请求数据,进行解析时抛出异常。 后端日志: params : {"start":["0"],"length":["15"],"searchMap":["[object Object]"]} 抛出异常: JSON parse error: syntax error, expect {, actual error, pos 0, fastjson-version 1.2.41; nested exception is com.alibaba.fastjson