Android JSON 解析模型显示 ClassCastException:com.google.gson.JsonObject 无法转换为 com.google.gson.JsonArray
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 class 并将键作为 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 class 看起来像这样 -
public class RestaurantsZomato {
public NearbyRestaurants nearby_restaurants;
public static class NearbyRestaurants {
public List<RestaurantDetails> restaurant;
}}
RestaurantDetails class 包含 "restaurant" 标签内的所有内容。我的问题是如何表示模型中 JsonResponse 中存在的“1”、“2”、“3”等 class。
如果这个问题很愚蠢,我很抱歉。正如我所说,我是 Android 开发的新手,任何能为我指明正确方向的资源都将不胜感激!
问题是您在 "nearby_restaurants" 中接收到一个 JSON 对象,并试图将其作为 JSON 数组获取。正如您在 Zomato 响应中看到的那样,没有 [],因此没有 json 数组对象。
在Json格式中这样
{
"nearby_restaurants":{
"1":{
"restaurant":{
}
},
"2":{
"restaurant":{
}
}
}
}
对应于包含两个对象“1”和“2”的对象 "nearby_restaurants",这两个对象都包含具有空值的对象 "restaurant"。
您正在尝试填充 java 列表对象,但在您的 Json 中不存在任何列表或数组!
看看this and this,你的问题就解决了。 :-)
编辑
试试这个代码
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());
}
public 列出< RestaurantDetails> 餐厅; return数组
public 餐厅详情 餐厅; return 对象
你的APIreturn对象。不是 return 数组
试试这个!!
public class RestaurantsZomato {
public NearbyRestaurants nearby_restaurants;
public static class NearbyRestaurants {
public RestaurantDetails restaurant;
}}
更新
您的 Json 字符串 return Json 对象。您必须使用 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
}
});
我是 Android 开发的新手,我正在创建一个需要我使用 Zomato Rest API 的应用程序。我正在使用 Koush ION 库 https://github.com/koush/ion 来发送 http 请求和接收响应。为了使用这个库,我简单地创建了一个模型 java class 并将键作为 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 class 看起来像这样 -
public class RestaurantsZomato {
public NearbyRestaurants nearby_restaurants;
public static class NearbyRestaurants {
public List<RestaurantDetails> restaurant;
}}
RestaurantDetails class 包含 "restaurant" 标签内的所有内容。我的问题是如何表示模型中 JsonResponse 中存在的“1”、“2”、“3”等 class。
如果这个问题很愚蠢,我很抱歉。正如我所说,我是 Android 开发的新手,任何能为我指明正确方向的资源都将不胜感激!
问题是您在 "nearby_restaurants" 中接收到一个 JSON 对象,并试图将其作为 JSON 数组获取。正如您在 Zomato 响应中看到的那样,没有 [],因此没有 json 数组对象。
在Json格式中这样
{
"nearby_restaurants":{
"1":{
"restaurant":{
}
},
"2":{
"restaurant":{
}
}
}
}
对应于包含两个对象“1”和“2”的对象 "nearby_restaurants",这两个对象都包含具有空值的对象 "restaurant"。
您正在尝试填充 java 列表对象,但在您的 Json 中不存在任何列表或数组!
看看this and this,你的问题就解决了。 :-)
编辑
试试这个代码
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());
}
public 列出< RestaurantDetails> 餐厅; return数组
public 餐厅详情 餐厅; return 对象
你的APIreturn对象。不是 return 数组
试试这个!!
public class RestaurantsZomato {
public NearbyRestaurants nearby_restaurants;
public static class NearbyRestaurants {
public RestaurantDetails restaurant;
}}
更新
您的 Json 字符串 return Json 对象。您必须使用 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
}
});