Reputation: 2395
I'm using Retrofit to make a few api calls. For a particular endpoint, the returned json looks a bit like this:
Endpoint: api.example.com/1.0/userinfo?userid=7
The returned response looks a bit like this:
{
"7":{
"name":"george",
"age"="32"
}
}
Basically, the top level object is whatever number gets passed into the url parameter (in this example 7).
So when creating my Java objects to model this response, how do I model this top level object so that even if the name changes it maps properly when using gson?
Upvotes: 2
Views: 416
Reputation: 56
Interface:
@Get
Call<Map<String,User>> getUserInfo(@Url String url);
Usages:
Map<String,User> response =getUserInfo("http://api.example.com/1.0/userinfo?userid=7");
User user = response.get("7");
"7" is userid=?
Interface:
@Get("1.0/userinfo")
Call<Map<String,User>> getUserInfo(@Query("userid")String userid);
Usages:
String userId = "7";
Map<String,User> response = Retrofit.Builder().baseUrl("http://api.example.com").create(ApiService.class).getUserInfo(userId).execute();
User user = response.get(userId);`
Upvotes: 1