gson

Deserializing JSON into a class with a generic argument using GSON or Jackson

南楼画角 提交于 2019-12-11 13:44:08
问题 I'm getting responses from my server that look like: { "timestamp" : 1, "some other data": "blah", "result" : { ... } } for a variety of calls. What I want to do client side is: class ServerResponse<T> { long timestamp; T result; } and then deserialize that with GSON or Jackson. I've been unable to do so thanks to type erasure. I've cheated that using subclasses like this: class SpecificResponse extends ServerRequest<SpecificType> {} but that requires a bunch of useless classes to lie around.

Gson:java.lang.StackOverflowError: null

只愿长相守 提交于 2019-12-11 13:30:32
问题 i have a class Delete which i want to convert it into json using Gson library but when i convert it it throws exception of java.lang.StackOverflowError: null here is my class import models.UserNotifications.MailMessages.DeleteReason._ import models.UserNotifications.MailMessages.DeleteStatus._ @SerialVersionUID(1) class Delete extends Serializable { var deleteStatus : DeleteStatus = DELETED var deleteReason : DeleteReason = EXPIRED /* * Setters */ def setDeleteStatus(deletestatus : String)= {

Json String contain integer value , while deserialize to HashMap, Integer convert into double values

可紊 提交于 2019-12-11 12:58:31
问题 public static void main(String[] args) { Gson g = new GsonBuilder() .setPrettyPrinting() .enableComplexMapKeySerialization() .serializeSpecialFloatingPointValues() .setLongSerializationPolicy(LongSerializationPolicy.DEFAULT) .setPrettyPrinting() //.registerTypeAdapter(HashMap.class, new HashMapDeserializer()) .create(); HashMap<Object, Object> h = new HashMap<Object, Object>(); h.put("num1", 10); h.put("num2", 20); h.put("num3", 20.0); h.put("num4", "<>"); h.put("num5", "~!@#$%^&*()_+=-`,.<>?

Volley and Gson -Sending a POST request using JSONObjectRequest

别说谁变了你拦得住时间么 提交于 2019-12-11 12:52:51
问题 I'm new here and I don't very well speak English, but I do my best to explain me. I have some problems with the Post request. This is my code: private void makeRequest() { String url = "http://diegocardenas.netau.net/comprobar_usuario.php"; JsonObjectRequest request1=new JsonObjectRequest(Method.POST, url, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { onConnectionFinished(); Usuario usuario = new Gson().fromJson(response.toString(), Usuario

Parse JSON file with Gson

夙愿已清 提交于 2019-12-11 12:27:03
问题 I have a method that gets the strongest Wifi acces points signal, which avialabe is and returns SSID string, all these SSID strings are stored in the raw folder in JSON file: How can I access the file in the raw folder and parse it with Gson to get for example the route_number 6 if the SSID "FR WLAN" is? ssid_number JSON file: { "data": [ { "ssid": "KD Privat", "route_number": 1 }, { "ssid": "KD WLAN Hotspot", "route_number": 4 }, { "ssid": "FR WLAN", "route_number": 6 } ] } WifiJSON class:

Parsing data with gson from google places

左心房为你撑大大i 提交于 2019-12-11 12:13:27
问题 I started using the Google Places api by calling the following url https://maps.googleapis.com/maps/api/place/textsearch/json?query=restaurants+in+Sydney&sensor=true&key=AddYourOwnKeyHere I found a post here on stackoverflow that showed the the basic structure of classes I need to to parse the json with gson. Here is what I have so far. public class GoogleMapper { @SerializedName("debug_info") private List<String> debug_info; @SerializedName("html_attributions") private List<String> html

Parse json array with different objects to their classes

谁说我不能喝 提交于 2019-12-11 12:08:03
问题 I have the following json: [ { "type": 1, "Steps": { "steps": steps } }, { "type": 2, "HeartRate": { "heartrates": heartRates } } ] The field steps is just an int an heartRates is an array of int's. I want to parse this using gson to two classes. I found a similar question on stackoverflow: How parse json array with multiple objects by gson? and tried it but it didn't work. This is my code: public class DataModels extends ArrayList<DataModels.Container> { public class Container { public int

How to escape slashes in Gson

风格不统一 提交于 2019-12-11 11:53:10
问题 According to the json specs, escaping " / " is optional. Gson does not do it by default, but I am dealing with a webservice which is expecting escaped " / ". So what I want to send is " somestring\\/someotherstring ". Any ideas on how to achieve this? To make things clearer: if I try to deserialize " \\/ " with Gson, it will send " \\\\/ ", which is not what I want! 回答1: Answer: The Custom Serializer You can write your own Custom Serializer - I have created one which follows the rule that you

GSON: Deserializing Json with random class names

浪子不回头ぞ 提交于 2019-12-11 11:42:53
问题 I'm trying to use GSON to deserialize some Json to a nice, tidy object. Now, I have managed to get the Json to correctly map to some of the more obvious variables. However, while trying to map some of the Json I came across this: { "this_number": 1, "that_number": 12, "some_string": "String!", "list_of_objects": { "342356676784653234535345": { "random_double": "0.1235667456456", "magic": "29", "health": 1, "price": 7, "point": { "x": 2, "y": 70 } }, "2345263767467354": { "random_double": "0

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

北慕城南 提交于 2019-12-11 11:33:31
问题 I'm getting the following error com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $ There are some other solutions out there, but none have worked for me. Most seem to be talking about having to make Call<WordOfTheDay> into Call<List<WordOfTheDay>> , but since the json is not an array that does not help. I believe it's the JSON, but am not sure. JSON {"id":520687,"word":"imbricated","contentProvider":{"name":