嗨我有json,它有一些动态字段名称:
{
"status": {
"rcode": 200,
"message": "OK"
},
"data": {
"0": {
"SubFranchiseID": "0",
"OutletID": "607",
"OutletName": "Spill "
},
"1": {
"SubFranchiseID": "0",
"OutletID": "32",
"OutletName": "PizzaRoma"
}
},
"hash": "b262c62ea3c8c693ad35210289a487d6963434d7"
}
“0”和“1”是动态字符串值。
从这里How to parse Dynamic lists with Gson annotations?
我创建了我的Data类:
public class Data {
public Map<String, Restaurant> restaurants = new HashMap<>();;
public Map<String, Restaurant> getRestaurants() {
return restaurants;
}
}
但是当我在Retrofit中使用getRestaurants()。size()时,我得到零值。
应该如何更改我的数据类?
编辑:
我的主要课程:
public class MyClass{
private Status status;
private Data data;
private String hash;
public Data getData() {
return data;
}
// other getter and setters
}
Retrofit和RxJava:
RetrofitService service1 = ServiceFactory.createRetrofitService(RetrofitService.class, RetrofitService.API_ENDPOINT);
service1.data()
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<MyClass>() {
@Override
public final void onNext(MyClass response) {
if(response.getData().getRestaurants().size() == 0)
Log.d("1", "size is ZERO ");
}
});
答案 0 :(得分:2)
解决方案是不使用Data类,而是将MyClass更改为:
public class MyClass{
private Status status;
@SerializedName("data")
private Map<String, Restaurant> data;
private String hash;
public Map<String, Restaurant> getData() {
return data;
}
// other getter and setters
}
答案 1 :(得分:0)
以下两个课程应该适合你。
class Data {
Map<String, List<Outlet>> types;
}
class Outlet {
String SubFranchiseID;
String OutletID;
String OutletName;
}