2016-05-06 65 views
0

我對Android開發相當陌生,我創建的應用程序需要我使用Zomato Rest API。我正在使用Koush ION庫https://github.com/koush/ion發送http請求並接收響應。爲了使用這個庫,我只需創建一個模型java類,其中的鍵爲java實例變量並調用這一行代碼。JSON解析的Android模型顯示ClassCastException:com.google.gson.JsonObject無法轉換爲com.google.gson.JsonArray

 Ion.with(getActivity()) 
        .load("https://developers.zomato.com/api/v2.1/geocode?lat=25.12819&lon=55.22724") 
        .setHeader("user-key",my-user-key) 
        .asJsonArray() 
        .setCallback(new FutureCallback<JsonArray>() { 
         @Override 
         public void onCompleted(Exception e, JsonArray result) { 
          if (e != null) { 
           Log.d("FoodFragment", "Error loading food"+e.toString()); 
           Toast.makeText(getActivity(), "error loading food", Toast.LENGTH_LONG).show(); 
           return; 
          } 
          else{ 
           Log.d("FoodFragment", "Size= " + result.size() + "; " + result.toString()); 
           sTrips = (List<RestaurantsZomato>) new Gson().fromJson(result.toString(), new TypeToken<List<RestaurantsZomato>>() { 

           }.getType()); 
           adapter = new RestaurantAdapter(sTrips); 
           recyclerView.setAdapter(adapter); 
           mSwipeRefreshLayout.setRefreshing(false); 
           ((RestaurantAdapter) adapter).setOnItemClickListener(new RestaurantAdapter.MyClickListener() { 
            @Override 
            public void onItemClick(int position, View v) { 
             Toast.makeText(getActivity(), "Item Clicked", Toast.LENGTH_LONG).show(); 
            } 
           }); 
          } 
         } 
        }); 
     } 

由於每Zomato API文檔,響應將是這個樣子 -

{"nearby_restaurants": { 
"1": { 
    "restaurant": { 

    } 
}, 
"2": { 
    "restaurant": { 
    } 
}, 
"3": { 
    "restaurant": { 

    } 
}, 
"4": { 
    "restaurant": { 

    } 
}, 
"5": { 
    "restaurant": { 

    } 
}, 
"6": { 
    "restaurant": { 

    } 
}, 
"7": { 

    } 
}, 
"8": { 
    "restaurant": { 

    } 
}, 
"9": { 
    "restaurant": { 

    } 
} 

而且我RestaurantZomato類看起來是這樣的 -

public class RestaurantsZomato { 
      public NearbyRestaurants nearby_restaurants; 
      public static class NearbyRestaurants { 
        public List<RestaurantDetails> restaurant; 
    }} 

而且RestaurantDetails類有裏面的一切「餐廳」標籤。我的問題如何表示在我的Model類的JsonResponse中存在的「1」,「2」,「3」等。

對不起,如果這個問題很蠢。正如我所說我是Android開發的新手,並且任何能夠指引我朝着正確方向的資源都將非常感謝!

+0

附加GSON 2.2.3.jar庫項目 –

+0

@DaminiMehra謝謝你的評論。然而我的課有import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.google.gson.reflect.TypeToken;這是否意味着圖書館已經存在? –

回答

0

公共名單< RestaurantDetails>餐廳;返回Array

public 餐廳詳情餐廳;返回對象

您的API返回對象。不返回陣列

試試這個!

public class RestaurantsZomato { 
     public NearbyRestaurants nearby_restaurants; 
     public static class NearbyRestaurants { 
       public RestaurantDetails restaurant; 
}} 

更新

你的JSON字符串返回的JSONObject。您必須使用的JSONObject

Ion.with(context) 
.load("https://developers.zomato.com/api/v2.1/geocode?lat=25.12819&lon=55.22724") 
.asJsonObject() 
.setCallback(new FutureCallback<JsonObject>() { 
@Override 
public void onCompleted(Exception e, JsonObject result) { 
    // do stuff with the result or error 
} 
}); 
+0

你好!謝謝您的回覆。我試過這個。不過,我仍然得到相同的錯誤。我認爲這與JSON響應中的「1」,「2」,「3」有關,因爲餐廳對象在另一個JSON對象內。我無法理解如何爲此創建實例變量。 –

+0

我更新了我的答案 –

+0

非常感謝!這解決了我的問題! –

0

問題是,您正在接收「nearby_restaurants」內的JSON對象,並試圖將其作爲JSONArray獲取。正如你在Zomato響應中看到的,沒有[]所以沒有json數組唯一的對象。

+0

感謝您對JSON對象和數組之間的簡單明瞭區分。然而,將其更改爲公共RestaurantDetails餐廳;沒有解決我的問題。我仍然收到同樣的錯誤。 –

1

JSON格式此

{ 
    "nearby_restaurants":{ 
     "1":{ 
     "restaurant":{ 

     } 
     }, 
     "2":{ 
     "restaurant":{ 

     } 
     } 
    } 
} 

對應於包含兩個對象的「1」和「2」既包含對象「餐廳」空值的對象「nearby_restaurants」。

您正在嘗試填充java List對象,但在您的Json中不存在任何列表或數組!

看看thisthis你會解決你的問題。 :-)

編輯

試試這個代碼

JsonObject jsonObject = yourJsonElement.getAsJsonObject(); 
JsonObject nearby_restaurants = jsonObject.get("nearby_restaurants").getAsJsonObject(); 
Set<Entry<String, JsonElement>> objects = nearby_restaurants.entrySet(); 

Gson gson = new Gson(); 

for (Entry<String, JsonElement> entry : objects) { 
    System.out.println(entry.getKey() + "/" + entry.getValue()); 
} 
+0

謝謝你的鏈接!不過,我仍然不確定如何在我的模型中表示鍵值「1」,「2」,「3」等,因爲我的模型將JSON的鍵值作爲其實例變量名稱。例如,如果JSON是{「用戶名」:「viji」}那麼我的模型具有公共字符串用戶名; –

相關問題