2016-06-26 25 views
2

Retrofitを使用して入れ子になっているアイテムオブジェクトを解析します。 これは私のオブジェクトAndroidでRetrofitを使用して入れ子になったjsonオブジェクトを解析する方法

 { 
     "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); 

この

は「BEGIN_ARRAYた期待BEGIN_OBJECTが、」あなたはエラー状態を見ることができるように、私は

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

エラーを報告できますか? – maddesa

+0

@maddesaありがとう。問題のエラースニペットを追加しました。あなたはそれを見ることができますか? –

答えて

0

を取得していますエラーです。あなたのRetroDataクラスでは、 "items"は文字列とMenu(オブジェクト)型のマップになると期待していますが、レスポンスでMenuオブジェクトを取得しないと、MenuItemのArrayを取得します。この作品

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

希望:

はこれを試してみてください。 P.P.私は個人的にそれをテストしていません。

+0

ありがとうShashank。出来た。 (y)私はこの非常にばかげた間違いをしていました。 –

関連する問題