gson

GSON - Optional and required fields with naming policy

喜欢而已 提交于 2019-12-24 09:49:50
问题 I need a function, that reads a json file and control the structur of the json file. Required fields should be defined. For that I found a question that resolve a part of my problem Gson optional and required fields. But in this case the naming convention has not power any more. In my case I used following GsonBuilder: this.gsonUpperCamelCase = new GsonBuilder() .registerTypeAdapter(TestClass.class, new AnnotatedDeserializer<TestClass>()) .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL

unnamed json array into java

柔情痞子 提交于 2019-12-24 09:04:06
问题 I have the following string: "[{\"type\":\"string\",\"value\":\"value1\",\"field\":\"label\"}, {\"type\":\"string\",\"value\":\"value2\",\"field\":\"address_unique\"}]" and I want to turn this into java GSON JsonObject array/list in order to invoke something like: myJsonObjs[i].getAsJsonPrimitive("value").getAsString(); What is the shortest path to achieve this? I tried setting up a bean like this: public static class Filter { @SerializedName("type") private String type; @SerializedName(

Compilation error: No source code is available for type com.google.gson.Gson

梦想的初衷 提交于 2019-12-24 08:47:32
问题 I'm trying to parse a json string on client side using Gson but while installing project getting an error: No source code is available for type com.google.gson.Gson; did you forget to inherit a required module? I have added <inherits name='com.google.gson.Gson' /> on my gwt.xml. I'm using GWT 2.8.1 can we use Gson on client side ? [this][1] using Gson library in GWT client code used older version of GWT and my question is also related to error of Gson module . 回答1: Gson is not GWT compatible.

Storing arbitrary data with GSON and Hibernate

夙愿已清 提交于 2019-12-24 08:39:15
问题 I want to persist some data relevant for the client only. I'm going to intentionally ignore database normalization here, as the data is pretty useless on the server side. I can do it trivially by making the client convert it to JSON and include the String in the JSON send in the request. This feels pretty wrong. I tried to do something smarter and failed badly. What I'd like to have: Given class MyEntity { String someString; int someInt; @Lob String clientData; } and an input { someString:

Java Gson http response difficult parsing

心不动则不痛 提交于 2019-12-24 08:38:56
问题 I want to parse big response from http json response from server. Creating class responding to file is harmful because file is too large. I tried with gson, but with no effect Here is response http://www.sendspace.pl/file/ff7257d27380cf5e0c67a33 And my code : try { JsonReader reader = new JsonReader(content); JsonElement json = new JsonParser().parse(reader); reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if (name.equals("devices")) { System.out.println(

Trouble retrieving data from web service using GSON

落花浮王杯 提交于 2019-12-24 07:09:54
问题 I have tried a lot of samples and tutorials about GSON and how things would work using it such as: http://www.softwarepassion.com/android-series-parsing-json-data-with-gson/ http://androidsmith.com/2011/07/using-gson-to-parse-json-on-android/ and etc. My problem is that I have this json value returned by my http://localhost:3000/users/1.json , which is: { "created_at": "2012-09-20T01:43:15Z", "id": 1, "name": "kevin", "updated_at": "2012-09-20T01:43:15Z" } Another is in this url http:/

JSON Array on Objects to List of String

可紊 提交于 2019-12-24 06:45:00
问题 I have the following JSON [{ "rowId": "03 EUR10580000" }, { "rowId": "03 EUR10900001" }, { "rowId": "03 EUR1053RUD*" }, { "rowId": "033331" }] and I would like to convert it to a List of String with only the values of the rowId, so in this case like "03 EUR10580000" "03 EUR10900001" "03 EUR1053RUD*" "033331" I did it with Gson fromJson but in return I get a List of LinkedTreeMap and when I do a loop in fails. I would like instead a simple List of String. 回答1: Well, your string is not a json

Log response body in case of exception

强颜欢笑 提交于 2019-12-24 06:41:55
问题 I am using retrofit for http calls with gson as a converter. In some cases I get exceptions thrown when gson tries to convert response to object and I would like to know what is the actual response in such case. For example: This is the exception message I get: Expected a string but was BEGIN_OBJECT at line 1 column 26 path $[0].date The code that execute the call is like this: Gson gson = gsonBuilder.create(); Retrofit retrofit = (new retrofit2.Retrofit.Builder()).baseUrl(baseUrl)

NullPointerException deserializing with gson

好久不见. 提交于 2019-12-24 06:40:36
问题 I have a JSON that contains the following: {"p1":{"debris":{"titanium":0,"silicum":0}},"p2":{"debris":{"titanium":0,"silicum":0}}... I declare the data using HashMap: public class Galax { public HashMap <String, InnerObject> pos; } public class InnerObject { public Debris debris; } public class Debris { public double titanium, silicum; } And I call the data using this: Galax galax = new Gson().fromJson(strGalaxy, Galax.class); System.out.println(galax.pos.get("p2").debris.titanium); The

GSON throws Expected BEGIN_ARRAY but was BEGIN_OBJECT error [duplicate]

不羁的心 提交于 2019-12-24 06:34:55
问题 This question already has answers here : GSON throwing “Expected BEGIN_OBJECT but was BEGIN_ARRAY”? (5 answers) Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 (little edit) (5 answers) Closed 3 years ago . I am using retrofit and parse result with GSON. Don't know why but I receive this one Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path I know what this error mean but don't see the reason. This is my json response {"suggestions": [ { "value": "г Казань",