Parse Dynamic Key Json String using Retrofit
Your resultInside
class is adding an extra object layer that does not exist in your JSON. Try moving the map to your Data
class results
field.
public class Data {
@SerializedName("results")
@Expose
private Map<String, Vitals> result;
//....
}
how to parse dynamic key response using retrofit
You need to change the response body type of Call<Data> call = apiService.getChampionData();
The response cannot be parsed to parsed one Data object, instead you should create a new response object like
public class DataResponse {
private Map<String, Data> data;
}
and call it Call<DataResponse> call = apiService.getChampionData()
;
Retrofit parse JSON dynamic keys
You could make your model POJO contain a Map<String, Champion>
to deserialize into, to deal with the dynamic keys.
Example:
public class ChampionData {
public Map<String, Champion> data;
public String type;
public String version;
}
public class Champion {
public int id;
public String title;
public String name;
public String key;
}
I'm not familiar with Retrofit besides that, but as someone in the comments said, the deserializing is done by Gson:
public ChampionData champions = new Gson().fromJson(json, ChampionData.class);
So to build on to the answer someone else posted, you can then do the following, assuming you've added the GsonConverterFactory
:
public interface API {
@GET("path/to/endpoint")
Call<ChampionData> getChampionData();
}
How to parse json with dynamic keys using Retrofit 2.0?
You can use Map<String, ModelClassName>
in your model class for dynamic like below :-
public class Data {
@SerializedName("your_key")
@Expose
private Map<String, ModelClassName> result;
//....
}
this can help to parse dynamic key in retrofit.
How to parse json array having dynamic keys using Retrofit 2
This is not a valid Json as you can try that on https://jsonlint.com/ , you can change your response to :
[
{
"type":"video",
"format":"mp4",
"size":"10mb"
},
{
"type":"audio",
"format":"mp3",
"size":"10mb"
},
{
"type":"text",
"format":"pdf",
"size":"10mb"
}
]
Retrofit gson with dynamic keys
Personally I'd drop the CityResponse
model and just work with Map<String, CityDetails>
.
The retrofit interface can then be:
public interface CityService {
@GET("/SAGetStrollAwayCity")
Call<Map<String, CityDetails>> getCities();
@GET("/SAGetStrollAwayCity")
Call<Map<String, CityDetails>> getCities(@Query("tagged") String tags);
}
In your scenario, the response body is empty because CityResponse
would map to a json like:
{
"city": {
"dynamic1": {
"cityID": "id1",
"priceRange": 15
},
"dynamic2": {
"cityID": "id2",
"priceRange": 15
}
}
}
However, you don't have a root element called city
. The json itself can already be mapped to a java Map
.
Hope this helps.
Related Topics
How to Hide Soft Keyboard When Activity Starts
How to Switch Activity Without Animation in Android
How to Implement a Long Click Listener on a Listview
Google Sign in Not Working After Publishing in Play Store
How to Check Current Running Applications in Android
Start_Sticky Does Not Work on Android Kitkat
Android - iPhone Style Tabhost
Turn Off Autosuggest for Edittext
Android: Accessing Assets Folder SQLite Database File with .Sqlite Extension
Android Asynctask Example and Explanation
Recyclerview Horizontal Scroll Snap in Center
How to Set a Reminder in Android
Connecting to MySQL from Android with Jdbc
Remove Data from Notification Intent
"Gradle Version 2.10 Is Required." Error