gpt4 book ai didi

java - 使用java在eclipse中读取JSON文件时获取空值

转载 作者:行者123 更新时间:2023-12-02 06:34:11 25 4
gpt4 key购买 nike

我正在读取放入项目文件夹中的 JSON 文件。我没有收到文件不存在的错误,在添加行以忽略未知值之前,我收到了错误。所以我很确定它正在读取值。我遇到的问题是,当我尝试从 JSON 文件打印变量时,我得到空值。

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,它为所有获取返回 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"
}
]

}

我做错了什么?

最佳答案

正如 @Jonathan 指出的那样,您实际上拥有一个带有单个键和一个数组作为值的 Map ,而不是在 FoodItemData.json 文件中包含一个对象。因此,您至少可以这样做(使用 Gson lib com.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) {
....
}
}
}

另请注意,一旦执行此操作,因为在您的 json 文件中国家/地区以 - 开头,它将无法进入

关于java - 使用java在eclipse中读取JSON文件时获取空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19822779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com