gson

Exclude null objects from JSON array with Gson

坚强是说给别人听的谎言 提交于 2019-12-23 03:59:10
问题 I'm using Gson to parse my REST API calls to Java objects. I want to filter out null objects in an array, e.g. { list: [ {"key":"value1"}, null, {"key":"value2"} ] } should result in a List<SomeObject> with 2 items. How can you do this with Gson? 回答1: Answer: The Custom Serializer You can add a custom serializer for List.class which would look like: package com.dominikangerer.q27637811; import java.lang.reflect.Type; import java.util.Collections; import java.util.List; import com.google.gson

Java JSON with GSON

為{幸葍}努か 提交于 2019-12-23 03:54:12
问题 Here's the problem, I'm using a weather APi from Wunderground and am having trouble using GSON to fetch the weather. import java.net.*; import java.io.*; import com.google.gson.*; public class URLReader { public static URL link; public static void main(String[] args) { try{ open(); read(); }catch(IOException e){} } public static void open(){ try{ link = new URL("http://api.wunderground.com/api/54f05b23fd8fd4b0/geolookup/conditions/forecast/q/US/CO/Denver.json"); }catch(MalformedURLException e

How to determine class of object from GSON parse?

*爱你&永不变心* 提交于 2019-12-23 03:53:16
问题 I am parsing JSON string from a byte-array and casting it as an object. How do I determine the class of the object? Object objDeserialized = gson.fromJson(jsonFromString, Object.class); //It could be type Message or RoomDetail 回答1: gson.fromJson(jsonFromString, Object.class); In general, this won't work because of Object.class . Gson prohibits overriding the Object class deserialization and uses ObjectTypeAdapter (see the primary Gson constructor as of Gson 2.8.0 and probably much earlier): /

Parsing JSON response using GSON with dynamic Key

亡梦爱人 提交于 2019-12-23 03:42:11
问题 This is how my JSON looks like and I have to parse the JSON, how can this be done using GSON { "data": { "a": { "abc": { "c": "d" } } } } In which "a" is dynamic key which may vary from time to time. I am unable to find a solution right now 回答1: Model public class Model { private HashMap<String, String> data; public Model() { } } Convert json string to Hashmap using Gson & prepare data from hashmap Gson gson = new Gson(); Type typeHashMap = new TypeToken<Map<String, String>>(){}.getType();

How to use gson TypeAdapter in Retrofit 2?

混江龙づ霸主 提交于 2019-12-23 03:34:16
问题 I have a working code wherein my retrofit client can retrieve a List of objects(country) from an api. The problem is, the parameter used by the api returns an ARRAY if I used to retrieve all the countries then it returns a single OBJECT when I want to query for a single country. As a result the following exception is shown. java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 4 column 17 path $.RestResponse.result By the way, I'm using Retrofit 2. I tried the

Trying to include GSON in Play! 2.0 project

时光怂恿深爱的人放手 提交于 2019-12-23 01:36:10
问题 I've added "com.google.gson" % "gson" % "2.2" to my AppDependencies but when I run play update, I get: [info] Updating {file:/Users/levinotik/Dev/scala/throw-away/myFirstApp/}myFirstApp... [warn] module not found: com.google.gson#gson;2.1.0 [warn] ==== local: tried [warn] /Users/levinotik/dev/scala/play-2.0.4/framework/../repository/local/com.google.gson/gson/2.1.0/ivys/ivy.xml [warn] ==== Typesafe Releases Repository: tried [warn] http://repo.typesafe.com/typesafe/releases/com/google/gson

How to send JSON response using socket in Java

不打扰是莪最后的温柔 提交于 2019-12-23 01:19:39
问题 I have built a basic HTTP server and I am attempting to send a reply to a GET request. I have installed Gson as the JSON parser, but I'm not sure how to encode the response in JSON and send back to the client. Here is my code, any help is much appreciated. (last method for actual response) public class HttpServer extends Thread{ //Private variables private Socket connectedClient = null; private BufferedReader clientRequest = null; private DataOutputStream responseToClient = null; /** * Public

How to use of XmlGregorianCalendar with fromJson and toJson methods of gson?

烈酒焚心 提交于 2019-12-22 18:26:36
问题 The theme of my project is to give XML format of data and get Json format using google-gson and I have JAXB generated java POJOs from XML schema in which I have a variable of XMLGregorianCalendar datatype. I give the following input of XML and get the json format from the gson.toJson() method; <?xml version="1.0" encoding="UTF-8"?> <EmpRequest xmlns="http://java.com/Employee"> <EmplIn> <EmpID>12</EmpID> <Empname>sara</Empname> <Designation>SA</Designation> <DOJ>2002-05-30T09:30:10+06:00</DOJ>

Parsing json directly using input stream

梦想与她 提交于 2019-12-22 18:22:25
问题 I am doing a api call and in response i am getting json. So for that I want to parse it directly through input stream so that there would not be need of storing it in memory. For this I am trying to use JSONReader but that i am unable use for api's less than 11. So i dont know how to proceed with. I want it to be done from 2.0 version onwards. Even parsing through JsonReader is not working. I was thing of having GSON parser but i am not getting how to implement the same with Inputstream. EDIT

Using proguard with GSON and RoboGuice fails when using a EnumTypeAdapter

情到浓时终转凉″ 提交于 2019-12-22 17:19:08
问题 When trying to use a EnumTypeAdapter with Proguard, it results in a AssertionError in Gson.getAdapter(). This error seems to be due to type information being lost... below is all the relevant source code: The Exception: 03-18 13:27:12.905: ERROR/roboguice(12139): Throwable caught during background processing java.lang.AssertionError at com.google.gson.internal.bind.TypeAdapters$EnumTypeAdapter.<init>(Unknown Source) at com.google.gson.internal.bind.TypeAdapters$24.create(Unknown Source) at