gson

Problems organizing complex json response with Realm

时光毁灭记忆、已成空白 提交于 2019-12-11 23:45:27
问题 I am trying to use realm with my android application. I tried looking in to some simple examples about realm. I was able to setup the project with realm and now want to start implementing it but facing problems to generate classed in accordance to my json response. following is an example of json I will be using { "Status":true, "Message":"Success", "ds":{ "Table1":[{ "CustomerId":"1", "CustomerName":"TestUser", "CustomerNo":"100001", "CustomerUrl":"http://abc-001-site10.itempurl.com",

gson model for json array without key

别说谁变了你拦得住时间么 提交于 2019-12-11 20:48:05
问题 I have the following json from our customer: { "id": 1234, "delivery_date": 1234567890, "actions": [ [ "foo", true], [ "bar", true] ], "customer": { "id": 12345, "company": "", "firstname": "John", "lastname": "Smith", "action": ["dothis", true] }, "childs": [ 123abc2132312312,11232432943493] } I want to parse the "actions" array as a List< Actions> actionList and the single "action" as Action action. With class Action { String action; boolean yesno; } And the childs Array as List< Child>

java.lang.ClassNotFoundException: com.google.gson.Gson

三世轮回 提交于 2019-12-11 19:39:34
问题 When I send a response from servlet to ajax, I get ClassNotFoundException about Gson class. Here is the stacktrace: java.lang.ClassNotFoundException: com.google.gson.Gson at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559) at com.jotform.demo.write(demo.java:64) at com.jotform.demo.doPost(demo.java:53) at javax.servlet.http.HttpServlet.service(HttpServlet.java:647) at

converting json string with Gson for generic model

本秂侑毒 提交于 2019-12-11 19:26:20
问题 i have class like this public class Response<T>{ T item; List<T>; } so how can to deserializeding String in this pattern to one generic class with gson.fromJson(targetString,...) ? i glad to used in retrofit library where Response is returned parameter in desired interface 回答1: convert list to gson public <T> String setList(List<T> list) { Gson gson = new Gson(); return gson.toJson(list); } convert gson to list public List<(desired class)> getList(){ Gson gson = new Gson(); String json =

GSON parse generic Json Array

社会主义新天地 提交于 2019-12-11 19:19:13
问题 My question is very similiar to this: Unable to parse Json array using Gson But I cann't get the answer from it. The answer from above link: public static List<MapData> getData(){ Gson gson = new Gson(); String jsonString = "[{\"id\":18,\"city\":\"test\",\"street\":\"test 1\",\"zipcode\":121209,\"state\":\"IL\",\"lat\":32.158138,\"lng\":34.807838},{\"id\":19,\"city\":\"test\",\"street\":\"1\",\"zipcode\":76812,\"state\":\"IL\",\"lat\":32.161041,\"lng\":34.810410}]"; Type type = new TypeToken

Serialize/Deserialize a POJO contain a speciel Enum (not Enum of String) with GSON

时光毁灭记忆、已成空白 提交于 2019-12-11 18:56:26
问题 I need Serialize/Deserialize a POJO contain a speciel Enum ( not Enum of String ). I find lots of sample with Enum of String but is not my case. I read Gson docs, and I have a start of solution with implements JsonDeserializer<T>, JsonSerializer<T> public class ApplicationError { private static final long serialVersionUID = 1L; private final ErrorCode code; private final String description; private final URL infoURL; .... } public enum ErrorCode { INVALID_URL_PARAMETER(HttpStatus.BAD_REQUEST,

How to check empty array string before creating json object in java?

微笑、不失礼 提交于 2019-12-11 18:29:54
问题 I'm getting an empty array in string as a response from server & getting ClassCastException while converting it JsonObject because it's an empty array. Here is code snippet. final String errorMessage = IOUtils.toString(errorStream); // response is "[]" if (isJson(errorMessage)) { final JsonObject jsonResult = new Gson().fromJson(errorMessage, JsonObject.class); throw new IOException(jsonResult.get("error").toString()); } else { throw new IOException("Json response is" + errorMessage); } Here

JsonSyntaxException Gson: Expected BEGIN_OBJECT but was STRING

*爱你&永不变心* 提交于 2019-12-11 18:29:22
问题 I have been trying to decrypt json data and then convert it to Scala case class using GSON. Code: import com.google.gson.Gson object DecryptQRData extends App { val key = "klix8TW3SMGtHLVO0ZbhwO8ggW0p+npHfB71epkvmE0=" val data = "c3NbCs3KQ6uC9d1kplSOg78opMs5+jKB/n5O3D4ttLQaNfWeU/s3RxYqDMazA09gOhhUWkRHZo3xrsgv/x2zoQ==" val bytesArray: Array[Byte] = AES.decodeBase64(data) val finalData: Array[Byte] = AES.decrypt(bytesArray, key) val jsonString: String = new String(finalData) println(s"result:

GsonBuilder serializer mess up with Timestamp and Date

心已入冬 提交于 2019-12-11 17:49:02
问题 I have used Gson to serialize and deserialize model class to json string , but the problem is gson takes date variabless to timestamp serializer.My class contain both Date and Timestamp variables but both are serialized by the same Timestamp serializer.I have wrote both Timestamp and date serializer in my GsonBuilder.Below is my class StaffDetails.java : import javax.persistence.*; import java.io.Serializable; import java.sql.Timestamp; import java.util.Date; import java.util.Locale; @Entity

Error on retrieving JSON field in Retrofit 2.0 [duplicate]

半城伤御伤魂 提交于 2019-12-11 17:47:53
问题 This question already has answers here : Gson, how to deserialize array or empty string (6 answers) Closed last year . I have a problem. I'm using Retrofit 2.0 to make calls to my api from my Android app. All works fine, but when I'm receive a empty field I get this error: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 8599 path $.meta.pagination.links The problem is with links field, that sometimes is not empty "links": { "next": "http://www.example.com, } But when it's empty the