简体   繁体   English

如何在Android中使用Retrofit解析嵌套的json对象

[英]How to parse a nested json object using Retrofit in Android

I want to parse a nested items object using Retrofit. 我想使用Retrofit解析嵌套项目对象。 this is my object 这是我的对象

        {  
       "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...
    }

And i made my retrofit models as below 我做了如下的改造模型

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;


}

But still i am getting an error when i try to parse this data. 但是,当我尝试解析此数据时,仍然出现错误。 Can anyone help me with this ? 谁能帮我这个 ? I am not sure where i am doing it wrong. 我不确定我在哪里做错了。

This is How i am building my Api call.. 这就是我建立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);

This is the error I am getting 这是我得到的错误

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

As you can see the error states "Expected BEGIN_OBJECT but was BEGIN_ARRAY". 如您所见,错误状态为“预期BEGIN_OBJECT,但之前为BEGIN_ARRAY”。 In your RetroData class, you expect "items" to be the map of string and Menu(object) type, but in the response you don't get Menu object, you get Array of MenuItems. 在RetroData类中,您期望“ items”是字符串和Menu(object)类型的映射,但是在响应中您没有得到Menu对象,而是得到了MenuItems的Array。

Try this: 尝试这个:

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;
}

Hope this works. 希望这行得通。 PS I have not tested it personally. PS我还没有亲自测试过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM