【问题标题】:Getting null values when reading in JSON file in eclipse using java使用java在eclipse中读取JSON文件时获取空值
【发布时间】:2013-11-06 21:04:42
【问题描述】:

我正在阅读我放在项目文件夹中的 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"
  }
]

}

我做错了什么?

【问题讨论】:

  • 请编辑并添加您的json文件的内容
  • 很可能您的 FoodItemData 与 FoodItemData.json 文件中的内容不匹配。发布该文件的内容将有助于查看是否属于这种情况
  • @user1631616 我将 Java 文件的名称从 FoodItemData 更改为只是 FoodItem,它仍然得到一个空值

标签: java json eclipse string


【解决方案1】:

正如@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 文件中,国家以 - 开头,它不会进入

【讨论】:

    【解决方案2】:

    您的文件包含 FoodItemData[],而不是 FoodItemData(另请参阅 http://www.w3schools.com/json/json_syntax.asp 并搜索数组)。该数组显然遗漏了 FoodItemData 的所有属性,结果对象为所有属性返回 null。同时,禁用 FAIL_ON_UNKNOWN_PROPERTIES 不会导致尝试从中生成 FoodItemData 时出现异常。

    【讨论】:

    • 你能再解释一下吗,我一直在玩 FoodItemData 作为一个数组,但仍然得到一个空返回值
    猜你喜欢
    • 2017-11-09
    • 1970-01-01
    • 2015-06-14
    • 2016-10-15
    • 1970-01-01
    • 1970-01-01
    • 2011-04-20
    • 2017-01-22
    • 1970-01-01
    相关资源
    最近更新 更多