Json parsing using Gson and Retrofit (Custom Deserializer) - android

I need to build list of places (from response array create instance of place and finally receive list of places (place in json))?
How to parse it using Gson and Retrofit with custom deserializer?
I have following strucutre:
{
"success": true,
"error": null,
"response": [
{
"loc": {
"lat": 51.50853,
"long": -0.12574
},
"place": {
"name": "London",
"state": "",
"stateFull": "",
"country": "GB",
"countryFull": "United Kingdom",
"region": "",
"regionFull": "",
"continent": "eu",
"continentFull": "Europe"
},
"profile": {
"elevM": 21,
"elevFT": 69,
"pop": 7556900,
"tz": "Europe/London",
"tzname": "BST",
"tzoffset": 3600,
"isDST": true,
"wxzone": null,
"firezone": null,
"fips": null,
"countyid": null
}
},
.............
.............
]
}

You can use Android Studio plugin RoboPOJOGenerator. It is very easy to make model classes from data.
This answer tells how to handle List response in retrofit.
Update
I don't think it is good idea to make custom deserializer just to parse a list. When you can filter or map list after getting response. It will take upto 3-4 lines of code.
If you don't want many classes. then you can safely delete Profile.java and Loc.java in your case, Gson will parse only data that you have declared in your pojo.
Make generic response class
You can make single class for handling all response by using Java Generics. See example.
public class ApiResponse<T> {
#SerializedName("error")
#Expose
private String error;
#SerializedName("success")
#Expose
private boolean success;
#SerializedName("response")
#Expose
private T response;
// make getter setters
}
Now in ApiService you can define response type. Like this
#GET("api/users/login")
Call<ApiResponse<ModelUser>> getUser();
#GET("api/users/login")
Call<ApiResponse<ModelCity>> getCity();
Thus you don't have to create 3 classes every time. This generic class will do work for all response.

Related

How to handle JSON array empty using Retrofit in Android

I am using Retrofit lib for API call in my Android application. My JSON is like this.
{"options": [
{
"type": "item",
"type_id": "19E9E453-64C6-41C9-BFB7-D6EFB8AF68B8",
"key": "ageRange",
"value": "Early 30s"
},
{
"type": "item",
"type_id": "19E9E453-64C6-41C9-BFB7-D6EFB8AF68B8",
"key": "colors",
"value": "Black, Red"
}
]}
Or
{"options": []}
My model code is:
#SerializedName("options")
#Expose
private ArrayList<OptionsDataModel> options;
So if options is not empty the code working fine but if options is empty I am getting error invalid item. So can any one help to solve this problem.
You can add a check in your response method for result.body != null before parsing your response to your object

Deserializing different objects in JSON array (GSON)

The JSON result for getUsers I get from the server looks like this:
{
"result": [
{
"meta": {
"rows": "3"
}
},
{
"items": [
{
"id": "1",
"name": "Steve",
"age": "30"
},
{
"id": "2",
"name": "Mary",
"age": "29"
},
{
"id": "3",
"name": "Bill",
"age": "58"
}
]
}
]
}
How can I deserialize it by GSON in my android app (I'm using retrofit)?
I can't imagine any wrapper classes because of the different object types in result.
Any help would be appreciated!
For good example
Converting JSON to Java
Other way, you can convert your json to a java object
Please use org.json library http://www.json.org/java/index.html
Then, for example
json = new JSONObject(YOUR_JSON).getJSONObject("result");
JSONArray items = data.getJSONArray("items");
String name = items.getJSONObject(0).getString("name");
You can write a TypeAdapter for a type that will be (de)serialized to(from) array. You can even make it generic, so it will work with type like Pair<A, B>. Here is an example for non-generic type: https://github.com/cakoose/json-tuple-databinding-examples/blob/master/java/src/GsonEntryCustomizer.java — it (de)serializes Entry to(from array).
Disclaimer — I have not written nor tested that code, but it seems legit.
If you only encounter such problem once (like in your example), you may not bother making it generic, just write TypeAdapter for your specific pair of 2 different classes. The reading code is quite straightforward:
in.beginArray();
SomeClass1 info1 = gson.getAdapter(SomeClass1.class).read(in);
SomeClass2 info2 = gson.getAdapter(SomeClass2.class).read(in);
in.endArray();
return new SomeContainerClass(info1, info2);
(see https://github.com/cakoose/json-tuple-databinding-examples/blob/master/java/src/GsonEntryCustomizer.java#L52)

How to parse multiple nested JSON objects/arrays with GSON?

I have been trying to wrap my head around how to parse nested objects and arrays with GSON, still stuck. How can I parse the nested items listed in the "results" array?
{
"item": {
"results": [
{
"__metadata": {
"url": "google.com",
"type": "website"
},
"listed": true,
"market": 225,
"town": "Toronto"
},
{
"__metadata": {
"url": "twitter.com",
"type": "website"
},
"listed": true,
"market": 225,
"town": "Calgary"
}
]
}
}
How can I easily do this with GSON within Android?
Thank you!
Android Studio
Download Plugin "GsonFormat"
Create your model class
Open Code->Generate->Gson
Paste your json click ok - if json is valid then it will convert the following json to java class (pojo)
Now create Gson object
Gson gson=new Gson();
Convert Json to java object
T obj = gson.fromJson(contents, tClass);
Now use this object "obj" to get values

Retrofit POJO is null but JSON is valid

Have been scratching my head on this one. The JSON Response is a valid one:
{
"MRData": {
"xmlns": "http://ergast.com/mrd/1.4",
"series": "f1",
"url": "http://ergast.com/api/f1/current/2.json",
"limit": "30",
"offset": "0",
"total": "1",
"RaceTable": {
"season": "2014",
"round": "2",
"Races": [
{
"season": "2014",
"round": "2",
"url": "https://en.wikipedia.org/wiki/2014_Malaysian_Grand_Prix",
"raceName": "Malaysian Grand Prix",
"Circuit": {
"circuitId": "sepang",
"url": "http://en.wikipedia.org/wiki/Sepang_International_Circuit",
"circuitName": "Sepang International Circuit",
"Location": {
"lat": "2.76083",
"long": "101.738",
"locality": "Kuala Lumpur",
"country": "Malaysia"
}
},
"date": "2014-03-30",
"time": "08:00:00Z"
}
]
}
}
}
The POJO for the response:
public class ApiResponse {
MRData mrdata;
public class MRData {
String xmlns;
String series;
String url;
String limit;
String offset;
String total;
RaceTable raceTable;
}
}
The apiResponse object is always null. Anyone can point out what is wrong with the POJO object here?
Thanks.
I see a couple of potential issues here:
You are defining a class inside another class; I have never seen it done like this. You might want to separate in two different files.
Your variable names on your POJO should match the variable names on the JSON response exactly. For example:
public class ApiResponse {MRData MRData;}
If you want your POJO's variables to be different than what the JSON sends back, you should use #SerlizedName.

Is this a valid JSON?

I am successfully parsing a json which looks like this, which in this particular case represents an array with 2 items:
{
"items": [
{
"id": 1,
"name": "John"
},
{
"id": 2,
"name": "Mark"
}
]
}
However, I can't figure out how to parse one like this:
{
"items": {
"1": {
"id": 1,
"name": "John"
},
"2": {
"id": 2,
"name": "Mark"
}
}
}
As you can see, the second json is pretty similar to previous one, except after items it begins with "{", which means it is an object not an array, but I need that data to treat like an array.
Is this an invalid json, or I am missing something?
EDIT:
Ok, got it. The json is valid.
Let me please reformulate the question.
What I am actually looking for is how could I get the second json into a list of items.
Somewhere in the code I am doing this:
ItemsResponse itemsResponse = JsonMarshaller.fromJson(ItemsResponse.class, response);
and the ItemsResponse class:
public class ItemsResponse {
private List<Item> items;
// getters and setters
}
but it fails with an exception which says that the beginning of the json is an object, not an array.
SOLUTION:
The correct way is to use a Map instead of the List:
public class ItemsResponse {
private Map<String, Item> items;
// getters and setters
}
Read the answer of Ahmad Dwaik 'Warlock' here
You can check your json here

Categories

Resources