使用java在eclipse中读取JSON文件时获取空值
我正在读取我放在项目文件夹中的JSON文件。我没有收到文件不存在的错误,在我添加忽略未知值的行之前,我收到了错误。因此,我非常确定它正在读取中的值。我遇到的问题是,当我试图从JSON文件打印出变量时,会得到空值使用java在eclipse中读取JSON文件时获取空值,java,json,eclipse,string,Java,Json,Eclipse,String,我正在读取我放在项目文件夹中的JSON文件。我没有收到文件不存在的错误,在我添加忽略未知值的行之前,我收到了错误。因此,我非常确定它正在读取中的值。我遇到的问题是,当我试图从JSON文件打印出变量时,会得到空值 File jsonFile = new File("FoodItemData.json"); FoodItemData food = null; ObjectMapper mapper = new ObjectMapper(); mapper.configure(
File jsonFile = new File("FoodItemData.json");
FoodItemData food = null;
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
try {
food = mapper.readValue(jsonFile, FoodItemData.class);
System.out.println(food.getcountry());
System.out.println(food.getId());
System.out.println(food.getDescription());
System.out.println(food.getcountry());
System.out.println(food);
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(food.getcountry());
我调用FoodItemData类,它为所有get返回null
public class FoodItemData{
private String country;
private String category;
private String description;
private String id;
private String name;
private String price;
public String getcountry(){
return this.country;
}
public void setcountry(String country){
this.country = country;
}
public String getCategory(){
return this.category;
}
public void setCategory(String category){
this.category = category;
}
public String getDescription(){
return this.description;
}
public void setDescription(String description){
this.description = description;
}
public String getId(){
return this.id;
}
public void setId(String id){
this.id = id;
}
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
public String getPrice(){
return this.price;
}
public void setPrice(String price){
this.price = price;
}
}
这是我的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": "Ploughman’s Salad",
"description": "Pork Pie, Pickled Onions, Pickled relish Stilton and Cheddar cheeses and crusty French bread.",
"category": "Lunch",
"price": "10.95"
}
]
}
我做错了什么?您的文件包含
FoodItemData[]
,而不是FoodItemData(另请参见和搜索数组)。这个数组显然遗漏了FoodItemData的所有属性,结果对象为所有属性返回null
。同时,禁用“未知”属性上的FAIL_不会导致从中生成FoodItemData时出现异常。正如@Jonathan所指出的那样,如果在FoodItemData.json
文件中没有对象,那么实际上有一个Map
和一个键和一个数组作为值。因此,为了读取文件内容,您至少可以这样做(使用Gson
libcom.google.Gson.Gson
):
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(new File("./FoodItemData.json")));
Map<String, List<FoodItemData>> object = (new Gson()).fromJson(reader, new TypeToken<Map<String, List<FoodItemData>>>(){}.getType());
List<FoodItemData> data;
if (object.values().iterator().hasNext()){
data = object.values().iterator().next(); // this is you data in your case 3 FoodItemData entries
}
} catch (FileNotFoundException e) {
....
}finally{
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
....
}
}
}
BufferedReader读取器=null;
试一试{
reader=new BufferedReader(新文件阅读器(新文件(“./FoodItemData.json”));
映射对象=(new Gson()).fromJson(读取器,new TypeToken(){}.getType());
列出数据;
if(object.values().iterator().hasNext()){
data=object.values().iterator().next();//这是案例3 FoodItemData条目中的数据
}
}catch(filenotfounde异常){
....
}最后{
if(读卡器!=null){
试一试{
reader.close();
}捕获(IOE异常){
....
}
}
}
还请注意,一旦您这样做,因为在您的json文件中,国家以
-
开头,它将不会进入请编辑并添加json文件的内容。很可能您的FoodItemData
与FoodItemData.json文件中的内容不匹配。发布该文件的内容将有助于查看这是否为case@user1631616我将Java文件的名称从FoodItemData更改为just FoodItem,它仍然会得到一个空值您能再解释一下吗,我一直在将FoodItemData作为数组使用,但仍然会得到一个空返回值