Java 解析 Json Array 导致 This is not a JSON Array 异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19780576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parsing Json Array resulting in This is not a JSON Array exception
提问by Rajath
I am trying to parse a Json array which looks like this:
我正在尝试解析一个如下所示的 Json 数组:
{
"FoodItemData": [
{
"country": "GB",
"id": "100",
"name": "Steak and Kidney Pie",
"description": "Tender cubes of steak, with tender lamb kidney is succulent rich gravy. Served with a side of mashed potatoes and peas.",
"category": "Dinner",
"price": "15.95"
},
{
"country": "GB",
"id": "101",
"name": "Toad in the Hole",
"description": "Plump British Pork sausages backed in a light batter. Served with mixed vegetables and a brown onion gravy.",
"category": "Dinner",
"price": "13.95"
},
{
"country": "GB",
"id": "102",
"name": "Ploughmana?s Salad",
"description": "Pork Pie, Pickled Onions, Pickled relish Stilton and Cheddar cheeses and crusty French bread.",
"category": "Lunch",
"price": "10.95"
}
]
}
I am using Gson
to parse this Json.
我正在Gson
用来解析这个 Json。
URL url = getClass().getClassLoader().getResource("FoodItemData.json");
FileReader fileReader = null;
try {
fileReader = new FileReader(url.getPath());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
JsonReader jsonReader = new JsonReader(fileReader);
Gson gson = new Gson();
JsonParser parser = new JsonParser();
JsonArray Jarray = parser.parse(jsonReader).getAsJsonArray();
List<FoodItem> listOfFoodItems = new ArrayList<FoodItem>();
for (JsonElement obj : Jarray) {
FoodItem foodItem = gson.fromJson(obj, FoodItem.class);
listOfFoodItems.add(foodItem);
}
This code results in java.lang.IllegalStateException: This is not a JSON Array
exception. The FoodItem
class contains the variables with the same name as in the Json.
此代码导致java.lang.IllegalStateException: This is not a JSON Array
异常。本FoodItem
类包含具有相同名称的变量作为JSON。
public class FoodItem {
private String id;
private String name;
private String description;
private String category;
private String price;
private String country;
}
Am I missing anything here? I tried using the following code as given in this answer, but I got the same exception as the one mentioned in that question. Any help would be appreciated.
我在这里错过了什么吗?我尝试使用此答案中给出的以下代码,但我遇到了与该问题中提到的相同的异常。任何帮助,将不胜感激。
Gson gson = new GsonBuilder().create();
Type collectionType = new TypeToken<List<FoodItem>>(){}.getType();
List<FoodItem> foodItems = gson.fromJson(jsonReader, collectionType);
采纳答案by Sotirios Delimanolis
You will need to change this
你需要改变这个
JsonArray jarray = parser.parse(jsonReader).getAsJsonArray();
to
到
JsonArray jarray = (JsonArray) parser.parse(jsonReader).getAsJsonObject().get("FoodItemData");
Your JSON contains a JSON object at the root called FoodItemData
. That element contains the JSON array you are trying to map to a List<FoodItem>
您的 JSON 在根目录包含一个名为 的 JSON 对象FoodItemData
。该元素包含您尝试映射到的 JSON 数组List<FoodItem>
Alternatively, you could create a class that has a field called FoodItemData
that is a List<FoodItem>
或者,您可以创建一个有一个字段名为类FoodItemData
是一个List<FoodItem>
public class RootElement {
List<FoodItem> FoodItemData;
// the good stuff
}
And parse like so
并像这样解析
RootElement root = gson.fromJson(jsonReader, RootElement.class);
System.out.println(root.FoodItemData);
Also, note that Java convention states that variable names should start with a lower case character.
另请注意,Java 约定规定变量名应以小写字符开头。
回答by Eran Medan
As @Sotirios correctly mentioned, your JSON is an object that contains an array, and not a standalone array.
正如@Sotirios 正确提到的,您的 JSON 是一个包含数组的对象,而不是一个独立的数组。
I would do a slight change though (instead of casting I would use getAsJsonArray
):
不过,我会做一些细微的改变(而不是我会使用铸造getAsJsonArray
):
JsonArray Jarray = parser.parse(jsonReader).getAsJsonObject().getAsJsonArray("FoodItemData");