gson

How to use gson to convert json to arraylist if the list contain different class?

独自空忆成欢 提交于 2019-12-22 03:55:14
问题 I want to store an arraylist to disk, so I use gson to convert it to string ArrayList<Animal> anim=new ArrayList<Animal>(); Cat c=new Cat(); Dog d=new Dog(); c.parentName="I am animal C"; c.subNameC="I am cat"; d.parentName="I am animal D"; d.subNameD="I am dog"; anim.add(c); anim.add(d); Gson gson=new Gson(); String json=gson.toJson(anim); public class Animal { public String parentName; } public class Cat extends Animal{ public String subNameC; } public class Dog extends Animal{ public

Gson - Read a value with two different keys

≯℡__Kan透↙ 提交于 2019-12-22 01:33:45
问题 In my Android project I have two types of response where both response are identical except two keys . Response 1 {"fullName":"William Sherlock Scott Holmes","address":"221B Baker Street, London, England, UK","downloads":642,"rating":3,"repos":["https://link1","https://link2","https://link3"]} Response 2 {"name":"Sherlock","city":"London","downloads":642,"rating":3,"repos":["https://link1","https://link2","https://link3"]} If you see the responses only two key names are changing fullName/name

Json preprocessing performance issue

痞子三分冷 提交于 2019-12-22 01:19:22
问题 I have a question here on how to pre process java. I implemented some suggestions and i am able to get it to work. Only concern is performance. Json pre processing in java I experimented two ways: one way is using gson i dumped it into hash map. then in getters i look into map to read. It took 12 seconds to do it. I tried one more approach. I created two classes for two json formats. based on source i am using jaskson to map it to respective class.I created on normalized class( class with

Comparing two JSONs

我们两清 提交于 2019-12-21 20:57:10
问题 What's the best way to compare two JSON strings in Java? I want to be able to print out only the difference in key/values pairs. I'm using gson library to convert it to a map and do assert but it displays both JSONs: Type type = new TypeToken<Map<String, String>>() {}.getType(); Gson gson = new Gson(); Map<String, String> mapExpected = gson.fromJson(expectedJson, type); Map<String, String> mapResponse = gson.fromJson(jsonResponse, type); Assert.assertEquals(mapExpected, mapResponse); Is there

Android - save Object to SharedPreferences and get it anywhere in the app

邮差的信 提交于 2019-12-21 20:14:20
问题 In my app I have a custom User class which holds some regular data (name etc...). I need to save that object and get it anywhere and anytime in other pages of the app. I made a helper class public final class GeneralMethods with many methods which I use a lot (static, of course). In order to save the data Im using Gson library. I made this method: public static void saveData(Context con, String variable, String data) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences

Gson Exception on deserialize (no-args constructor does not exist)

橙三吉。 提交于 2019-12-21 19:46:37
问题 I've got a problem which occurs only in 10 of 5000 devices. There is no possibility to reproduce it with my emulators and test devices. It seems to be a very specific problem with only a few devices. All I've got is the stacktrace and my code. So I'm developing against a black hole and only after I released the new version at GooglePlay I see if the changes solved the problem. I just updated a new version to GooglePlay, but the error still exists. The changes were parameterless contructors

Retrofit How To Print Out Response JSON

Deadly 提交于 2019-12-21 17:50:29
问题 I'm using Retrofit and I want to get access to the JSON response that is returned from the server. Could someone please advise me. Thanks 回答1: if you only want to see the respones for debugging purpose just turn on debugging in retrofit and look at the log. It's something like: restAdapter.setDebuggingEnabled(true); If you need access to the direct response in code all of the time, you should be doing that at the httpclient level as it's basically bypassing the purpose of retrofit 回答2: To

JSON Serialize / Deserialize generic type with google-gson

泄露秘密 提交于 2019-12-21 17:23:42
问题 Well, I have to confess I'm not good at generic type in Java I've written a JSON serialize/deserialize class in C# using JavaScriptSerializer private static JavaScriptSerializer js = new JavaScriptSerializer(); public static T LoadFromJSONString<T>(string strRequest) { return js.Deserialize<T>(strRequest); } public static string DumpToJSONString<T>(T rq) { return js.Serialize(rq); } It works well in C#. Now I'm trying to convert, or atleast, write another JSON serialize/deserialize class in

Gson, JsonElement, String comparison in Java

余生颓废 提交于 2019-12-21 12:51:12
问题 Ok, I'm wondering this could be quite simple and stupid but after a while fighting with the situation I have no idea what is happening. I'm using Gson to handle a few JSON elements. Somewhere in my code I get one of the JsonElements of a JsonObject as a String and I compare it against another String. As far as I can see both of them are equals but when comparing I always get false. Here is the snippet. JsonArray arr; JsonObject jsonobj; JsonElement model_elem; String STUPID_STRING = "bla bla

Gson, JsonElement, String comparison in Java

我的梦境 提交于 2019-12-21 12:51:09
问题 Ok, I'm wondering this could be quite simple and stupid but after a while fighting with the situation I have no idea what is happening. I'm using Gson to handle a few JSON elements. Somewhere in my code I get one of the JsonElements of a JsonObject as a String and I compare it against another String. As far as I can see both of them are equals but when comparing I always get false. Here is the snippet. JsonArray arr; JsonObject jsonobj; JsonElement model_elem; String STUPID_STRING = "bla bla