简体   繁体   中英

How to parse a nested json object using Retrofit in Android

I want to parse a nested items object using 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..

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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