Android 改型,顶级json对象更改?
我正在使用改型来进行一些api调用。对于特定端点,返回的json看起来有点像:Android 改型,顶级json对象更改?,android,json,gson,retrofit,retrofit2,Android,Json,Gson,Retrofit,Retrofit2,我正在使用改型来进行一些api调用。对于特定端点,返回的json看起来有点像: { "7":{ "name":"george", "age"="32" } } 端点:api.example.com/1.0/userinfo?userid=7 返回的响应有点像这样: { "7":{ "name":"george", "age"="32" } } 基本上,顶级对象是传递到url
{
"7":{
"name":"george",
"age"="32"
}
}
端点:api.example.com/1.0/userinfo?userid=7
返回的响应有点像这样:
{
"7":{
"name":"george",
"age"="32"
}
}
基本上,顶级对象是传递到url参数中的任何数字(在本例中为7)
因此,在创建Java对象以模拟此响应时,如何对顶级对象进行建模,以便即使名称更改,在使用gson时也能正确映射?接口:
@Get
Call<Map<String,User>> getUserInfo(@Url String url);
@Get("1.0/userinfo")
Call<Map<String,User>> getUserInfo(@Query("userid")String userid);
@Get
调用getUserInfo(@Url字符串Url);
用途:
Map<String,User> response =getUserInfo("http://api.example.com/1.0/userinfo?userid=7");
User user = response.get("7");
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);`
Map response=getUserInfo(“http://api.example.com/1.0/userinfo?userid=7");
User=response.get(“7”);
“7”是userid=
接口:
@Get
Call<Map<String,User>> getUserInfo(@Url String url);
@Get("1.0/userinfo")
Call<Map<String,User>> getUserInfo(@Query("userid")String userid);
@Get(“1.0/userinfo”)
调用getUserInfo(@Query(“userid”)字符串userid);
用途:
Map<String,User> response =getUserInfo("http://api.example.com/1.0/userinfo?userid=7");
User user = response.get("7");
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);`
String userId=“7”;
映射响应=改装.Builder().baseUrl(“http://api.example.comcreate(ApiService.class).getUserInfo(userId.execute();
User=response.get(userId)`
我不确定我是否明白。为什么要使用地图
?我以前使用过十几次改型,但通常json是结构化的,因此顶级对象键是相同的。例如:顶层对象类似于“userinfo”:{“id”:88}
,我将使用id变量为userinfo创建一个模型类。但是既然在这种情况下顶层对象发生了变化,我该如何建模呢?