gson

Data Class Either Object or Array

别等时光非礼了梦想. 提交于 2020-01-06 08:26:14
问题 I have a Kotlin data class that has an arg that can either be an Object or Array. Is there a way to de-serialize a string into this class and not care if not an Array but somehow get it into an array of one? data class Game(var name:List<NameItem>) data class NameItem(var title: String, var id: Int) data can come back as both ways a single object or an array of objects( I have no control over the data as it is 3rd party data. jsonString = "{"game":{"name":{"title":"GameName","id":22}}}"

How to serialize DefaultMutableTreeNode (Java) to JSON?

馋奶兔 提交于 2020-01-06 07:59:07
问题 How can I serialize a tree (implemented in Java using the DefaultMutableTreeNode class) to JSON (for transferring via RESTful method to an iOS client)? I tried: String jsonString = (new Gson()).toJson(topNode); // topNode is DefaultMutableTreeNode at the root It crashed with StackOverflowError . 回答1: Swing's DefaultMutableTreeNode class is a tree-like data structure which contains instances of this same type both as children and as parent . That's why Gson's default serializer ran into

Handle JSON response with multiple type of the same name

主宰稳场 提交于 2020-01-06 04:42:18
问题 The JSON response is : { "success": false, "errorMessages": [ "You have to select a maximum load of <span style='color:red;'>0</span> Credit/Course but you have selected <span style='color:red;'>3</span> Credit/Course --- [R060]", "You can register courses as a full study with a load limit between <span style='color:red;'>12</span> and <span style='color:red;'>18</span> Credit/Course, but you have selected <span style='color:red;'>9</span> Credit/Course --- [R062]" ], "isConflict": 0 } but

How can I use gson to create a set of key value pairs?

允我心安 提交于 2020-01-06 02:31:08
问题 I currently have this code, how can I add to it so I can get a JsonObject from Gson to append it to an existing Json file? private static void writeFile(File f, String w_username, String w_password) throws IOException{ Gson gson = new Gson(); JsonWriter writer = new JsonWriter(new FileWriter(f)); } 回答1: JSON structure does not allow just to append more data at the end of the file. In this case more suitable could be CSV format. To solve your problem you need to read the whole file as

GSON de/serialization of wrapper class

守給你的承諾、 提交于 2020-01-05 15:16:03
问题 I have a convenience class wrapper for a Map, that looks something like this: class MapWrapper { private Map<String, Integer> wrapped = new HashMap<>(); public void add(String key, Integer count) {/*implementation*/} // Other modifiers } The reason that I'm not using a Map directly but rather the wrapper is because I need to use the methods to access the Map indirectly. When I de/serialize this object, I would like the JSON to serialize as if the wrapper class wasn't there. E.G. I want: {

GSON操作json

我只是一个虾纸丫 提交于 2020-01-05 12:36:28
添加依赖: implementation 'com.google.code.gson:gson:2.7' // 解析json import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import java.util.List; public class JsonUtil { static final private Gson gson = new Gson(); // javabean 转 json static public String ObjToStr(Object bean){ return gson.toJson(bean); } // json 转 javabean static public <T>T StrToObj(String jsonDate,Class objClass){ return (T) gson.fromJson(jsonDate,objClass); } // list 转 json static public String ListToStr(List list){ return gson.toJson(list); } // json 转list static public <T> List<T> StrToList(String jsonDate){

JSON和GSON操作json数据

心已入冬 提交于 2020-01-05 12:36:07
1,JSON操作json 1 import net.sf.json.JSONArray; 2 import net.sf.json.JSONObject; 3 4 //json操作数据 5 public static String objToJson(User user) 6 { 7 JSONObject jsonObject = JSONObject.fromObject(user); 8 return jsonObject.toString(); 9 } 10 public static User jsonToObj(String str) 11 { 12 JSONObject jsonObject = JSONObject.fromObject(str); 13 User user = (User)jsonObject.toBean(jsonObject, User.class); 14 return user; 15 } 16 public static JSONArray strToJsonArray(List<User> users) 17 { 18 return JSONArray.fromObject(users); 19 } 20 2,GSON操作json 1 import com.google.gson.Gson; 2 3 //gson操作数据 4 public

Failed to deserialize an object with List using Gson

↘锁芯ラ 提交于 2020-01-05 09:09:37
问题 I have the following exception while deserializing an JSON to object using Gson: com.google.gson.JsonParseException: The JsonDeserializer com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter@588722d6 failed to deserialized json object [{"time":1378911600000,"total":0},{"time":1378912500000,"total":0},{"time":1378913400000,"total":2,"sum":130000,"avgLen":65000.0}] given the type com.google.gson.ParameterizedTypeImpl@490ca2fa The class which should represent the JSON is: public class Api{

Replace a key in GSON

丶灬走出姿态 提交于 2020-01-05 08:10:43
问题 I'm new to GSON. I have a JSON object (As a String) - { "name" : "myName", "city" : "myCity" } I parsed this as follows - JsonParser parser = new JsonParser(); JsonObject json_result = (JsonObject)parser.parse(#TheAboveMentionedStringGoesHere); Now I want to replace the key name with something else ,say, firstName so that the resulting JSON object is - { "firstName" : "myName", "city" : "myCity" } Is this possible? How do I achieve this? 回答1: If you use com.google.code.gson:gson:2.+ Google

Replace a key in GSON

笑着哭i 提交于 2020-01-05 08:09:35
问题 I'm new to GSON. I have a JSON object (As a String) - { "name" : "myName", "city" : "myCity" } I parsed this as follows - JsonParser parser = new JsonParser(); JsonObject json_result = (JsonObject)parser.parse(#TheAboveMentionedStringGoesHere); Now I want to replace the key name with something else ,say, firstName so that the resulting JSON object is - { "firstName" : "myName", "city" : "myCity" } Is this possible? How do I achieve this? 回答1: If you use com.google.code.gson:gson:2.+ Google