0

プロジェクト フォルダーに配置した JSON ファイルを読み込んでいます。ファイルが存在しないというエラーは表示されず、不明な値を無視する行を追加する前にエラーが発生していました。したがって、値を読み取っていると確信しています。私が抱えている問題は、JSONファイルから変数を出力しようとすると、null値が得られることです。

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"
  }
]

}

私は何を間違っていますか?

4

2 に答える 2

1

@Jonathanが指摘したように、FoodItemData.jsonファイル内にオブジェクトを持つ代わりに、実際にはMap単一のキーと配列を値として持っています。したがって、ファイルの内容を読み取るためにできる最小のことは、( Gsonlibを使用して) 次のとおりです。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ファイルで国が始まるため、-それが取得されないことに注意してください

于 2013-11-06T21:44:17.117 に答える