gson

Gson and deserializing an array of objects with arrays in it

你说的曾经没有我的故事 提交于 2019-12-27 16:39:08
问题 I am trying to use Gson to deserialize a json string returned from my webservice The structure would be returned as TypeDTO[] . where TypeDTO is like int id; String name; ArrayList<ItemDTO> items[] and ItemDTO is like int id; String name; Boolean valid; When I call the code as follows Gson gson = new Gson(); TypeDTO[] mytypes = (TypeDTO[]) gson.fromJson(reply, TypeDTO[].class); Everything inside the objects is null However, If I use the JSONArray and JSONObject to pull them out piece by piece

Gson Serialize field only if not null or not empty

时光总嘲笑我的痴心妄想 提交于 2019-12-27 14:33:13
问题 I have requirement where I need to convert java object to json. I am using Gson for that but i need the converter to only serialize the non null or not empty values. For example: //my java object looks like class TestObject{ String test1; String test2; OtherObject otherObject = new OtherObject(); } now my Gson instance to convert this object to json looks like Gson gson = new Gson(); TestObject obj = new TestObject(); obj.test1 = "test1"; obj.test2 = ""; String jsonStr = gson.toJson(obj);

Parsing JSON array into java.util.List with Gson

醉酒当歌 提交于 2019-12-27 10:38:31
问题 I have a JsonObject named "mapping" with the following content: { "client": "127.0.0.1", "servers": [ "8.8.8.8", "8.8.4.4", "156.154.70.1", "156.154.71.1" ] } I know I can get the array "servers" with: mapping.get("servers").getAsJsonArray() And now I want to parse that JsonArray into a java.util.List ... What is the easiest way to do this? 回答1: Definitely the easiest way to do that is using Gson's default parsing function fromJson() . There is an implementation of this function suitable for

Parsing JSON array into java.util.List with Gson

回眸只為那壹抹淺笑 提交于 2019-12-27 10:37:04
问题 I have a JsonObject named "mapping" with the following content: { "client": "127.0.0.1", "servers": [ "8.8.8.8", "8.8.4.4", "156.154.70.1", "156.154.71.1" ] } I know I can get the array "servers" with: mapping.get("servers").getAsJsonArray() And now I want to parse that JsonArray into a java.util.List ... What is the easiest way to do this? 回答1: Definitely the easiest way to do that is using Gson's default parsing function fromJson() . There is an implementation of this function suitable for

Expecting a string but was BEGIN_OBJECT: JsonReader

混江龙づ霸主 提交于 2019-12-25 19:06:22
问题 There are some other answers to this question on here but not sure how to get them to work with my code. I have a lot of JSON to pass so I need to stream it rather than do it all at once. I cannot quite get the errors out of the code. Here is my code: HttpGet request = new HttpGet(getUri + "&" + credentials.getOauthStructure()); HttpResponse response = client.execute(request); ICustomerDAO customerDAO = (ICustomerDAO) DAOFactory.getDAO( ICustomerDAO.class.getName(), context); customerDAO.open

Expected BEGIN_ARRAY but was BEGIN_OBJECT with Gson and Retrofit

南笙酒味 提交于 2019-12-25 18:14:28
问题 My app try to consume an API using Retrofit and Gson . My request is to https://nahuatiliztli.herokuapp.com/laws.json and it returns back: [{"id":1,"title":"Constitución Política de los Estados Unidos Mexicanos","url":"http://www.diputados.gob.mx/LeyesBiblio/pdf/1_29ene16.pdf","created_at":"2016-07-10T02:36:00.641Z","updated_at":"2016-07-10T02:36:00.641Z"}] My code is structured as follow… My model is Law class: public class Law { @SerializedName("id") private Integer mId; @SerializedName(

Type of objects inside non-parameterized arrays in JSON

青春壹個敷衍的年華 提交于 2019-12-25 17:42:33
问题 I am trying to write simple function that converts Json strings into Objects. The language I am using is objective-c however this question discusses issues doesn't relate to this lang. My question is, How to know the Type of Objects that laid inside a json array that is to be mapped into non-parameterized (a.k.a non-generic) lists?? I found two Json Java libraries unable to solve this issue, Jakson and Gson and here's the example: import java.io.Serializable; import java.util.List; import com

Retrofit and Gson: parsing array/element-polymorphic objects

╄→гoц情女王★ 提交于 2019-12-25 17:17:45
问题 I am getting response in a sequence: "parameters": { "parameter": { "Data":"value" } }, "parameters":{ "parameter": [ { "Data":"value" }, { "Data":"value" }, ] }, Getting the error if I call List<Class> parameter: Expected BEGIN_OBJECT but getting BEGIN_ARRAY I need to parse parameter to get values public class ApiClient { public static final String BASE_URL ="http://........."; private static Retrofit retrofit = null; public static Retrofit getClient() { OkHttpClient client = new

Deserialize json string into generically typed list

馋奶兔 提交于 2019-12-25 16:59:26
问题 I want to include in my Service objects a generic way of deserializing lists of objects from a json string. Below was my first attempt. public abstract class AbstractService<T>{ public abstract Class<T> getClazz(); public List<T> deserialize(final String json){ Gson gson = gsonFactory.create(); Type listType = new TypeToken<List<T>>() {}.getType(); List<T> entityList = gson.fromJson(json, listType); return entityList; } } However due to type erasure, the T in: new TypeToken<List<T>>() {}

gson parse items from comma-separated-value string within a json value

我怕爱的太早我们不能终老 提交于 2019-12-25 16:42:37
问题 I am parsing a json string using gson . It is similar to this: { "ACode": "aa", "RCode": "rr", "Errors": "e1,e2,e3" } I think that the errors should have been a proper json array, but I don't have control of that. I want to get the errors into an array or collection in java . This is easy enough using String.split with comma as separator. However I am new to gson and I don't know if I would be ignoring functionality that it provides to parse a comma separated string. Does anyone know whether