我想使用Retrofit解析嵌套的items对象。 这是我的对象
{
"items":{
"Veg Main Course":[
{
"category":"27013046129796342702616905670",
"description":"",
"price":100.0,
"name":"Aloo 65"
},
{
"category":"27013046129796342702616905670",
"description":"",
"price":100.0,
"name":"Finger Chips"
},
{
"category":"27013046129796342702616905670",
"description":"",
"price":100.0,
}
],
"Bityanis":[
{
"category":"27013046129796342702616905670",
"description":"",
"price":100.0,
"name":"Aloo 65"
},
{
"category":"27013046129796342702616905670",
"description":"",
"price":100.0,
"name":"Finger Chips"
},
{
"category":"27013046129796342702616905670",
"description":"",
"price":100.0,
}
]
.
.
.
So on...
}
我制作了改装模型如下
public class RetroData {
public Map<String,Menu> items;
}
public class Menu {
public Map<String, ItemArray> cat_menu;
}
public class ItemArray {
public ArrayList<MenuItem> itemArray;
}
public class MenuItem {
public String category;
public String description;
public String price;
public String is_active;
public String id;
public String disabled;
public String sales_count;
public String image;
public String name;
}
但是当我尝试解析这些数据时,我仍然遇到错误。 谁能帮我这个 ? 我不确定我在哪里做错了。
这就是我如何构建我的Api电话..
public abstract interface RestService {
@GET("/MyApp/api/menu/all")
public abstract void getMenu(Callback<RetroData> response);
}
RestAdapter adapter = new RestAdapter.Builder()
.setEndpoint(ROOT_URL)
.build();
RestService api = adapter.create(RestService.class);
这是我得到的错误
06-26 22:16:21.136 4194-4194/com.example.adam.myfirstapp W/System.err: retrofit.RetrofitError: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 32 path $.items.
06-26 22:16:21.136 4194-4194/com.example.adam.myfirstapp W/System.err: at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:383)
06-26 22:16:21.136 4194-4194/com.example.adam.myfirstapp W/System.err: at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220)
06-26 22:16:21.136 4194-4194/com.example.adam.myfirstapp W/System.err: at retrofit.RestAdapter$RestHandler$2.obtainResponse(RestAdapter.java:278)
06-26 22:16:21.136 4194-4194/com.example.adam.myfirstapp W/System.err: at retrofit.CallbackRunnable.run(CallbackRunnable.java:42)
06-26 22:16:21.136 4194-4194/com.example.adam.myfirstapp W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-26 22:16:21.136 4194-4194/com.example.adam.myfirstapp W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-26 22:16:21.136 4194-4194/com.example.adam.myfirstapp W/System.err: at retrofit.Platform$Android$2$1.run(Platform.java:142)
06-26 22:16:21.136 4194-4194/com.example.adam.myfirstapp W/System.err: at java.lang.Thread.run(Thread.java:818)
06-26 22:16:21.136 4194-4194/com.example.adam.myfirstapp W/System.err: Caused by: retrofit.converter.ConversionException: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 32 path $.items.
06-26 22:16:21.137 4194-4194/com.example.adam.myfirstapp W/System.err: at retrofit.converter.GsonConverter.fromBody(GsonConverter.java:67)
06-26 22:16:21.137 4194-4194/com.example.adam.myfirstapp W/System.err: at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:367)
06-26 22:16:21.137 4194-4194/com.example.adam.myfirstapp W/System.err: ... 7 more
06-26 22:16:21.137 4194-4194/com.example.adam.myfirstapp W/System.err: Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 32 path $.items.
06-26 22:16:21.139 4194-4194/com.example.adam.myfirstapp W/System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:200)
06-26 22:16:21.140 4194-4194/com.example.adam.myfirstapp W/System.err: at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
06-26 22:16:21.140 4194-4194/com.example.adam.myfirstapp W/System.err: at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:187)
06-26 22:16:21.141 4194-4194/com.example.adam.myfirstapp W/System.err: at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:145)
06-26 22:16:21.141 4194-4194/com.example.adam.myfirstapp W/System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:103)
06-26 22:16:21.141 4194-4194/com.example.adam.myfirstapp W/System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:196)
06-26 22:16:21.141 4194-4194/com.example.adam.myfirstapp W/System.err: at com.google.gson.Gson.fromJson(Gson.java:810)
06-26 22:16:21.141 4194-4194/com.example.adam.myfirstapp W/System.err: at com.google.gson.Gson.fromJson(Gson.java:775)
06-26 22:16:21.141 4194-4194/com.example.adam.myfirstapp W/System.err: at retrofit.converter.GsonConverter.fromBody(GsonConverter.java:63)
06-26 22:16:21.141 4194-4194/com.example.adam.myfirstapp W/System.err: ... 8 more
06-26 22:16:21.141 4194-4194/com.example.adam.myfirstapp W/System.err: Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 32 path $.items.
06-26 22:16:21.141 4194-4194/com.example.adam.myfirstapp W/System.err: at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:387)
06-26 22:16:21.141 4194-4194/com.example.adam.myfirstapp W/System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:189)
06-26 22:16:21.141 4194-4194/com.example.adam.myfirstapp W/System.err: ... 16 more
答案 0 :(得分:0)
如您所见,错误状态为“预期BEGIN_OBJECT但是BEGIN_ARRAY”。在RetroData类中,您希望“items”是字符串和Menu(对象)类型的映射,但是在响应中您没有获得Menu对象,您将获得MenuItem的数组。
试试这个:
public class RetroData {
public Map<String,List<MenuItem>> items;
}
public class MenuItem {
public String category;
public String description;
public String price;
public String is_active;
public String id;
public String disabled;
public String sales_count;
public String image;
public String name;
}
希望这有效。 附:我没有亲自测试过。