gson

Deserialize json in java with gson

拟墨画扇 提交于 2020-01-01 19:53:10
问题 I am having a very tough time deserializing json in java with gson. I have the following json: {"races":[ {"id":1,"mask":1,"side":"alliance","name":"Human"}, {"id":2,"mask":2,"side":"horde","name":"Orc"}, {"id":3,"mask":4,"side":"alliance","name":"Dwarf"}]} The java code I have now is: StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() { @Override public void onResponse(String response) { Gson gson = new Gson(); Type type = new TypeToken

How to do conditional Gson deserialization default value

走远了吗. 提交于 2020-01-01 19:47:47
问题 Imagine if I have the following JSON {"game":"football", "people":"elevent"} {"game":"badminton", "people":"two"} My class as below class Sport { String game; String people; } I could do a deserialize of my Json as below Sport mySport = Gson().fromJson(json, Sport.class); However, if my JSON is only {"game":"football"} {"game":"badminton"} I would like it to automatically initialize people to "elevent" or "two", pending of the first field. Is there a way to configure my GsonBuilder() to have

Gson Json parser Array of Arrays

。_饼干妹妹 提交于 2020-01-01 10:08:35
问题 Looking to parse some Json and parse out array of arrays. Unfortunately I cannot figure out how to handle nested arrays within the json. json { "type": "MultiPolygon", "coordinates": [ [ [ [ -71.25, 42.33 ], [ -71.25, 42.33 ] ] ], [ [ [ -71.23, 42.33 ], [ -71.23, 42.33 ] ] ] ] } What I have implemented when I just an a single array. public class JsonObjectBreakDown { public String type; public List<List<String[]>> coordinates = new ArrayList<>(); public void setCoordinates(List<List<String[]>

Convert java Object to CSV

无人久伴 提交于 2020-01-01 08:48:49
问题 What API should I use to convert Java object to CSV. Can I use google-gson for java object conversion to CSV format? 回答1: Jackson provides for easy conversion to/from CSV (with Java and/or JSON). The post at http://www.cowtowncoder.com/blog/archives/2012/03/entry_468.html describes well how to use it. 回答2: please give me your specific requirements about GSON. Gson provides toJson() and fromJson() methods to convert Java objects to JSON and vice-versa. can u refer this link. This link gives

Proguard obfuscation results in java.lang.IllegalArgumentException: class declares multiple JSON fields named “a”

拈花ヽ惹草 提交于 2020-01-01 08:46:58
问题 Upon starting up my app logs two errors, but only after obfuscation. Without obfuscation there are no errors. Unless I'm being misled, the errors are like: class Property declares multiple JSON fields named a and same for class PropertyDefinition. 05/18 03:33:19.465 java.lang.IllegalArgumentException: class PropertyDefinition declares multiple JSON fields named a: com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:122) com.google.gson

Proguard obfuscation results in java.lang.IllegalArgumentException: class declares multiple JSON fields named “a”

…衆ロ難τιáo~ 提交于 2020-01-01 08:46:31
问题 Upon starting up my app logs two errors, but only after obfuscation. Without obfuscation there are no errors. Unless I'm being misled, the errors are like: class Property declares multiple JSON fields named a and same for class PropertyDefinition. 05/18 03:33:19.465 java.lang.IllegalArgumentException: class PropertyDefinition declares multiple JSON fields named a: com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:122) com.google.gson

NoSuchMethodError Lcom/google/gson/Gson; upon retrofit response

微笑、不失礼 提交于 2020-01-01 06:54:29
问题 So, I've had my app published for nearly a year without seeing this issue, and now it shows up. Even right now, I don't have this issue with the debug version on my phone. I don't have any issues with any emulators opened from Android Studio. However nearly every emulator from the pre-launch reports in the Google Developer Console crashes with this NoSuchMethodError upon receiving Retrofit response. FATAL EXCEPTION: ControllerMessenger Process: xxxxxxx, PID: 25090 java.lang.NoSuchMethodError:

OutOfMemoryError when using Gson to parse a large JSON response

你说的曾经没有我的故事 提交于 2020-01-01 03:32:05
问题 URL url = new URL("http://pubapi.cryptsy.com/api.php?method=orderdatav2"); CryptsyCurrencyPairsReturn response = gson.fromJson(new InputStreamReader(url.openStream()), CryptsyCurrencyPairsReturn.class); This results in an OutOfMemoryException for some of my users on older Android devices. How can I parse this large response without running out of memory? 回答1: Parsing large data in one-go is always tricky and troublesome. However Gson comes with some nice features to support that too. You

gson - How to include class name property when serializing object of any type

末鹿安然 提交于 2020-01-01 03:04:28
问题 Came to realize I need to include the class name as a property when serializing an object in my application. It would probably be best if I added the class name property for any non-primitive object that is serialized. I saw that this is a built-in feature in Genson with the useClassMetadata method. But I'm already using gson in my project, so it would be beneficial if I could stick with it. This is my current attempt: package com.mycompany.javatest; import com.google.gson.*; import java.lang

scala drools and map

无人久伴 提交于 2020-01-01 00:09:11
需求,安全检查,例如Linux系统,用户安全检查,配置项检查等,这些检查的规则在Drools里面去实现,数据传送过来即可, 问题:如何定义数据结构,不同的检查项会有不同的数据结构,如何规范呢? 思路: 使用map嵌套的思路,检查的数据输出过来是json的格式发送到kafka,然后spark streaming程序直接读取json,然后转为map进行处理。 遇到的问题,一开始代码是使用scala.util.parsing.json.JSON.parseFull(string),这样返回的可能是map[String,Any] 但在drools的rule文件中直接的语言是java,所以需要引入scala.collection.javaconversion._来隐式转换。 但这里的问题是,转换只会完成一层,但json会嵌套,会返回嵌套的map,这样这种方法就不好用了。 折腾了半天,暂时是两层,只是强制转换完成了,这里提一下,java中的object对应scala里面的AnyRef. val jsonstr = scala.io.Source.fromFile(new File("""C:\git\ccms-analyzer\target\classes\test\jsondemo.txt""")).mkString("") //use gson to parse json to map