![](/img/trans.png)
[英]com.google.gson.JsonObject cannot be cast to com.google.gson.JsonArray in Java
[英]Android Model for JSON parsing shows ClassCastException: com.google.gson.JsonObject can not be casted to com.google.gson.JsonArray
我是Android開發的新手,正在創建一個需要使用Zomato Rest API的應用程序。 我正在使用Koush ION庫https://github.com/koush/ion發送http請求並接收響應。 為了使用該庫,我只需創建一個模型Java類,並將鍵作為Java實例變量,然后調用此代碼行。
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開發的新手,任何向我指明正確方向的資源將不勝感激!
在Json格式中,
{
"nearby_restaurants":{
"1":{
"restaurant":{
}
},
"2":{
"restaurant":{
}
}
}
}
對應於包含兩個對象“ 1”和“ 2”的對象“ nearby_restaurants”,它們兩個都包含具有空值的對象“ restaurant”。
您正在嘗試填充java List對象,但是在Json中不存在任何列表或數組!
編輯
試試這個代碼
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());
}
問題是您正在“ nearby_restaurants”內部接收到一個JSON對象,並試圖將其作為JSONArray來獲取。 如您在Zomato響應中看到的,沒有[],因此沒有僅json數組的對象。
public List <RestaurantDetails>餐廳; 返回數組
公共RestaurantDetails餐廳; 返回對象
您的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
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.