gson

GSON throwing “Expected Expected a name but was NUMBER at line 1 column 8”?

耗尽温柔 提交于 2019-12-30 09:07:15
问题 I'm trying to parse a JSON string like this one ( generated URL with http://www.json-generator.com) { "total": 86, "jsonrpc": "2.0", "id": 1, "result": [ { "startDate": "14/03/2012", "meetingId": "1330", "creator": "Jhon", "lastModified": "02/04/2012", "meetingTitle": "task clarification", "location": "Conf hall", "startTime": "02:00 PM", "createdDate": "14/03/2012", "owner": "Peter", "endTime": "02:30 PM" }, { "startDate": "20/03/2012", "meetingId": "1396", "creator": "Mr.Hobbs",

com.google.gson.JsonSyntaxException when trying to parse Date/Time in json

别等时光非礼了梦想. 提交于 2019-12-30 07:59:48
问题 I'm using RestTemplete to get json data from a rest api and I'm using Gson to parse data from json format to Object Gson gson = new Gson(); restTemplate = new RestTemplate(); restTemplate.getMessageConverters().add(new GsonHttpMessageConverter()); restTemplate.getMessageConverters().add(new StringHttpMessageConverter()); List<Appel> resultList = null; resultList = Arrays.asList(restTemplate.getForObject(urlService, Appel[].class)); but I get this problem with Date, what should I do .. Could

Json to Gson - Modeling

删除回忆录丶 提交于 2019-12-30 07:31:29
问题 I'm trying to convert a JSON to GSON , but I can not model. Can anyone give me an example with this one. [ { "id": "1", "name": "lalala", "object1": [ "string1", "string1", "string1" ], "object2": [ "anotherString1", "anotherString2" ] }, { "id": "2", "name": "laaaaalala", "object1": [ "string1", "string1", "string1" ], "object2": [ "anotherString1", "anotherString2" ] } ] Thanks 回答1: Here you go. import java.io.FileReader; import java.util.List; import com.google.gson.Gson; public class Foo

Simpler use of TypeAdapterFactory

前提是你 提交于 2019-12-30 04:18:06
问题 AFAIK the most flexible gson customization is possible via TypeAdapterFactory , however it may get needlessly complicated. It forces me to write for each handled class both read and write , while sometimes only one method is really needed. Moreover, sometimes a JsonSerializer and/or JsonDeserializer were much easier to write, e.g. like here. This leads me to these questions: Is it possible to write a TypeAdapter which simply delegates one of its methods (e.g. writing of ImmutableList to

Best practice to Serialize java.time.LocalDateTime (java 8) to js Date using GSON

旧街凉风 提交于 2019-12-30 03:58:07
问题 In our recent project we use java 8. I need to serialize java.time.LocalDateTime to java script Date format. Currently what I did was define a custom serializer to convert LocalDateTime to timestamp. public class LocalDateTimeSerializer implements JsonSerializer<LocalDateTime> { @Override public JsonElement serialize(LocalDateTime localDateTime, Type type, JsonSerializationContext jsonSerializationContext) { Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant(); Date date

Gson serialize POJO with root value included?

送分小仙女□ 提交于 2019-12-30 01:40:07
问题 I'm having a problem serializing an object using Gson. @XmlRootElement class Foo implements Serializable { private int number; private String str; public Foo() { number = 10; str = "hello"; } } Gson will serialize this into a JSON {"number":10,"str":"hello"} . However, I want it to be {"Foo":{"number":10,"str":"hello"}} , so basically including the top level element. I tried to google a way to do this in Gson, but no luck. Anyone knows if there is a way to achieve this? Thanks! 回答1: You need

Retrofit - removing some invalid characters from response body before parsing it as json

ぐ巨炮叔叔 提交于 2019-12-29 06:24:09
问题 I have an external web service that in the response body returns json but nested in parentheses, like this: ({"door_x":"103994.001461","door_y":"98780.7862376", "distance":"53.3"}) Using this code: class AddressInfo { String door_x; String door_y; } interface AddressWebService { @GET("/reversegeocoding") AddressInfo reverseGeocoding(@Query("x") double x, @Query("y") double y); } It obviously fails. This is the stacktrace: retrofit.RetrofitError: com.google.gson.JsonSyntaxException: java.lang

Retrofit - removing some invalid characters from response body before parsing it as json

只愿长相守 提交于 2019-12-29 06:24:03
问题 I have an external web service that in the response body returns json but nested in parentheses, like this: ({"door_x":"103994.001461","door_y":"98780.7862376", "distance":"53.3"}) Using this code: class AddressInfo { String door_x; String door_y; } interface AddressWebService { @GET("/reversegeocoding") AddressInfo reverseGeocoding(@Query("x") double x, @Query("y") double y); } It obviously fails. This is the stacktrace: retrofit.RetrofitError: com.google.gson.JsonSyntaxException: java.lang

Issues with using Gson to pretty-print JSON String

若如初见. 提交于 2019-12-29 05:51:37
问题 Could someone please suggest why this is happening... I’ve got some code to pretty print some JSON. To do this, I am making use out of the Gson library. However, while thus usually works well, some characters don’t seem to be displayed properly. Here is a simple piece of code that demonstrates the problem: //Creating the JSON object, and getting as String: JsonObject json = new JsonObject(); JsonObject inner = new JsonObject(); inner.addProperty("value", "xpath('hello')"); json.add("root",

Reading JSON Content

时光总嘲笑我的痴心妄想 提交于 2019-12-29 05:50:28
问题 I'm using jsoup to scrape some HTML data and it's working out great. Now I need to pull some JSON content (only JSON, not HTML). Can I do this easily with jsoup or do I have to do it using another method? The parsing that jsoup performs is encoding the JSON data so it's not parsing properly with Gson. Thanks! 回答1: While great, Jsoup is a HTML parser, not a JSON parser, so it is useless in this context. If you ever attempt it, Jsoup will put the returned JSON implicitly in a <html><head> and