gson

Gson parse Json with array

隐身守侯 提交于 2019-12-31 03:12:09
问题 I am having some trouble building a class which will parse out gson as I expect it to. I created a class. public class JsonObjectBreakDown { public String type; public List<String> coordinates = new ArrayList<String>(); } And called JsonObjectBreakDown p = gson.fromJson(withDup, JsonObjectBreakDown.class); Below is my json { "type":"Polygon", "coordinates":[ [ [ -66.9, 18.05 ], [ -66.9, 18.05 ], [ -66.9, 18.06 ], [ -66.9, 18.05 ] ] ] } I have used gson before successfully, but never with an

Getting stackoverflowerror from Gson while converting hashmap to JSON object

泄露秘密 提交于 2019-12-31 03:05:09
问题 I want to represent data in tree structure as java object then I want to convert it to JSON object. With the help of stackoverflow entries: Convert java arrayList of Parent/child relation into tree? hashmap to JSON using GSON I had below main function and "pairs" list contains a pair: child and parent ArrayList<Pair> list= new ArrayList<>(); list.add(new Pair("6", "4")); list.add(new Pair("5", "4")); list.add(new Pair("4", "3")); list.add(new Pair("2", "3")); list.add(new Pair("3", "null"));

JSON Decode Custom Class with HashMap member using GSON in Java

…衆ロ難τιáo~ 提交于 2019-12-31 01:52:27
问题 I have the following class: class IndexItem { private String word; private HashMap<String, Integer> docs; private Integer total; public IndexItem(String word) { this.total = 0; this.docs = new HashMap<String, Integer>(); this.word = word; } public IndexItem() { this.total = 0; this.docs = new HashMap<String, Integer>(); this.word = ""; } } I also have the following JSON string encoded from one of this classes instances using GSON: {"word":"refer","docs":{"c84ada58bb47e7ee8fab14d6d0ae1978.html

How to escape the quotes in JSON Object?

你说的曾经没有我的故事 提交于 2019-12-30 22:54:08
问题 Below is my method which makes the JSONObject and then print out the JSONString . I am using Google GSON. private String generateData(ConcurrentMap<String, Map<Integer, Set<Integer>>> dataTable, int i) { JsonObject jsonObject = new JsonObject(); Set<Integer> ap = dataTable.get("TEST1").get(i); Set<Integer> bp = dataTable.get("TEST2").get(i); jsonObject.addProperty("description", "test data"); jsonObject.addProperty("ap", ap.toString()); jsonObject.addProperty("bp", bp.toString()); System.out

轻触开源(一)-Java泛型Type类型的应用和实践

柔情痞子 提交于 2019-12-30 19:14:32
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 转载请注明出处:https://my.oschina.net/u/874727/blog/747427 Q:1025250620 在很多Java的开源项目中都用到Java的泛型。比如Gson,就可以通过TypeToken<T>里的泛型参数来指定生成的类型。鉴于网上关于泛型的文章并不多,为了非墨后面项目研究的需要,非墨开始研究这部分的API。首先我们先来看一下在Java语言中泛型的例子: public class MyTest<T1,T2 extends Number> { T1 member; public <T> void method(T m) {} } 上述代码中的标志:T1,T2,T都是泛型类型。Java的泛型检查发生在编译期,但是会在编译后的JVM字节码中增加类型判断的语句。为了方便大家理解这句话我们用一段代码测试一下: List<String> list = new ArrayList<>(); try { Method m = list.getClass().getDeclaredMethod("add", new Class[]{Object.class}); m.invoke(list, 1); m.invoke(list, 2); } catch (Exception e) { System

Converting ZonedDateTime type to Gson

白昼怎懂夜的黑 提交于 2019-12-30 18:56:11
问题 I have rest service that return arraylist of object,and I have implemented jersy restful client to execute it,but I have problem in converting ZonedDateTime type to json so I get this error Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 231 path $[0].lastmodifieddate How can i fix this problem? lastmodifieddate column in entity @Column(name = "lastmodifieddate") private ZonedDateTime lastmodifieddate; //getter and setter rest service

Why does Gson parse an Integer as a Double?

a 夏天 提交于 2019-12-30 17:31:51
问题 A complex json string and I want to convert it to map, I have a problem. Please look at this simple test: public class Test { @SuppressWarnings("serial") public static void main(String[] args) { Map<String, Object> hashMap = new HashMap<String, Object>(); hashMap.put("data", "{\"rowNum\":0,\"colNum\":2,\"text\":\"math\"}"); Map<String,Object> dataMap = JsonUtil.getGson().fromJson( hashMap.get("data").toString(),new TypeToken<Map<String,Object>>() {}.getType()); System.out.println(dataMap

Gson parsing on android with proguard

∥☆過路亽.° 提交于 2019-12-30 13:41:33
问题 I only have a parsing problem with Gson when I use proguard. In debug mode and release mode without proguard, everything work fine. This is my json string : {"tables":[{"name":"Profile","columns":[{"label":"id","type":"text","primary":true},{"label":"name","type":"text"},{"label":"age","type":"integer"},{"label":"human","type":"integer"},{"label":"gear","type":"Gear","custom":true,"list":true}]},{"name":"Gear","columns":[{"label":"id","type":"text","primary":true},{"label":"type","type":"text

Gson parsing on android with proguard

你说的曾经没有我的故事 提交于 2019-12-30 13:41:14
问题 I only have a parsing problem with Gson when I use proguard. In debug mode and release mode without proguard, everything work fine. This is my json string : {"tables":[{"name":"Profile","columns":[{"label":"id","type":"text","primary":true},{"label":"name","type":"text"},{"label":"age","type":"integer"},{"label":"human","type":"integer"},{"label":"gear","type":"Gear","custom":true,"list":true}]},{"name":"Gear","columns":[{"label":"id","type":"text","primary":true},{"label":"type","type":"text

How to compare two JSON Strings for equality using GSON?

为君一笑 提交于 2019-12-30 09:55:34
问题 I am trying to compare two JSON Strings for equality. I found this solution which uses Jackson as shown below but in all my project I am using GSON so I need to do the same thing using GSON. ObjectMapper mapper = new ObjectMapper(); JsonNode tree1 = mapper.readTree(jsonString1); JsonNode tree2 = mapper.readTree(jsonString2); if (tree1.equals(tree2)) { // yes, contents are equal -- note, ordering of arrays matters, objects not } else { // not equal } Is there any way to compare two JSON String