gson

Parsing JSON with GSON

大城市里の小女人 提交于 2020-01-12 23:13:12
问题 I'm having some trouble with GSON, mainly deserializing from JSON to a POJO. I have the following JSON: { "events": [ { "event": { "id": 628374485, "title": "Developing for the Windows Phone" } }, { "event": { "id": 765432, "title": "Film Makers Meeting" } } ] } With the following POJO's ... public class EventSearchResult { private List<EventSearchEvent> events; public List<EventSearchEvent> getEvents() { return events; } } public class EventSearchEvent { private int id; private String title;

Gson - Serialize Nested Object as Attributes

亡梦爱人 提交于 2020-01-12 17:30:31
问题 Is there an easy way to convert how a nested Object is converted to JSON? I'm trying to just create one JSON object to match the back-end. I'm using Retrofit for my networking, which converts an Object to JSON with Gson. I don't have access to any code between network call and the convertion, so I'm trying to find a clean way to modify how the Object is converted, either through the GsonBuilder, or Annotations. // Automatically converted to JSON with passed in Gson. Call<myObject> search(

Deserializing Map<Object, Object> with GSon

爷,独闯天下 提交于 2020-01-12 15:12:29
问题 I have a Map containing a mixture of types like in this simple example final Map<String, Object> map = new LinkedHashMap<String, Object>(); map.put("a", 1); map.put("b", "a"); map.put("c", 2); final Gson gson = new Gson(); final String string = gson.toJson(map); final Type type = new TypeToken<LinkedHashMap<String, Object>>(){}.getType(); final Map<Object, Object> map2 = gson.fromJson(string, type); for (final Entry<Object, Object> entry : map2.entrySet()) { System.out.println(entry.getKey()

Deserializing Map<Object, Object> with GSon

风流意气都作罢 提交于 2020-01-12 15:12:11
问题 I have a Map containing a mixture of types like in this simple example final Map<String, Object> map = new LinkedHashMap<String, Object>(); map.put("a", 1); map.put("b", "a"); map.put("c", 2); final Gson gson = new Gson(); final String string = gson.toJson(map); final Type type = new TypeToken<LinkedHashMap<String, Object>>(){}.getType(); final Map<Object, Object> map2 = gson.fromJson(string, type); for (final Entry<Object, Object> entry : map2.entrySet()) { System.out.println(entry.getKey()

Convert class into a JSONObject

别说谁变了你拦得住时间么 提交于 2020-01-12 03:53:09
问题 I have several classes like this. I want to convert the classes into JSONObject format. import java.io.Serializable; import com.google.gson.annotations.SerializedName; public class User implements Serializable { private static final long serialVersionUID = 1L; @SerializedName("id") private Integer mId; @SerializedName("name") private String mName = ""; @SerializedName("email") private String mEmail; public Integer getId() { return mId; } public void setId(Integer id) { mId = id; } public

What's wrong with this deserialization from a JSON file to objects using Gson?

那年仲夏 提交于 2020-01-11 14:38:07
问题 So I have the following super class: public class Location { private String name; private double[] coordinates; private String description; } And the following subclasses: public class Monument extends Location { private String architect; private short inauguration; } public class Restaurant extends Location { private String[] characteristics; } public class Hotel extends Location { private short stars; } The JSON file I'm trying to deserialize is an array of Locations where each location can

How to create json object using com.google.gson class

让人想犯罪 __ 提交于 2020-01-11 14:00:14
问题 I'm trying to convert jsonArray with jsonObject by passing another jsonObject. Any sample code please? { "role": "CUSTOMER", "operationId": "updateHomePlace", "parameters": { "tcId": "f44015c8-d672-411b-a0f9-49cf9ef3f6b2", "otpVerification": "false", "password": "false", "homePlace": [{ "address": "MIG 528, KPHB 1st Phase,, Kukatpally Housing Board Colony, Kukatpally, Hyderabad, Telangana 500072, India", "lat": "17.4866943", "lng": "78.3994029" }] } } 回答1: Like this you can write POJO / Model

Retrofit, how to parse JSON objects with variable keys

心不动则不痛 提交于 2020-01-11 09:51:30
问题 First I know my title is bad as I didn't come up with better, I'm opened to suggestion. I'm using retrofit to get data from an api of this kind : @GET("users/{userid}") It works fine and I'm happy with it, the problem is when I call the same api with @POST("users/widget") with a list of ids. I have the following answer : { "long_hash_id": { "_id": "long_hash_id" ....... }, "long_hash_id": { "_id": "long_hash_id", ..... }, ........ } the "long_hash_id" is typicaly "525558cf8ecd651095af7954" it

Retrofit, how to parse JSON objects with variable keys

妖精的绣舞 提交于 2020-01-11 09:51:20
问题 First I know my title is bad as I didn't come up with better, I'm opened to suggestion. I'm using retrofit to get data from an api of this kind : @GET("users/{userid}") It works fine and I'm happy with it, the problem is when I call the same api with @POST("users/widget") with a list of ids. I have the following answer : { "long_hash_id": { "_id": "long_hash_id" ....... }, "long_hash_id": { "_id": "long_hash_id", ..... }, ........ } the "long_hash_id" is typicaly "525558cf8ecd651095af7954" it

Format of POJO for nested JSON?

眉间皱痕 提交于 2020-01-11 03:40:11
问题 So lets say the JSON response is: [{ "data" : { "item1": value1, "item2:" value2 }}] How do you get the values 'value1' and 'value2' when you must first access data? If the fields were at the root then I could just have the method return a POJO with those field names. I basically want the below to work. @GET("/path/to/data/") Pojo getData(); class Pojo { public String item1; public String item2; } 回答1: You can try below code to convert your json string to Pojo object with required fields