0

I am reading in a JSON file that I put in my project folder. I am not getting an error that the file doesn't exist and before I added the line to ignore unknown values I was getting errors. So I am pretty sure it is reading values in. The problem I am having is when I try to print out variables from the JSON file I get null values.

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());

I call the class FoodItemData and it returns null for all the gets.

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;
}

}

Here is my JSON file

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

}

What am I doing wrong?

3
  • 1
    Please edit and add the content of your json file Commented Nov 6, 2013 at 21:06
  • most likely you have a mismatch between your FoodItemData and what you have in the FoodItemData.json file. Posting a content of that file will help to see if this is a case Commented Nov 6, 2013 at 21:11
  • @user1631616 I changed the name of the Java file from FoodItemData to just FoodItem, it still get a null value Commented Nov 6, 2013 at 21:16

2 Answers 2

1

As @Jonathan pointed out instead of having an object inside of FoodItemData.json file you actually have a Map with single key and an array as value. So the minimum what you can do is this (using Gson lib com.google.gson.Gson) in order to read the content of the file:

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) {
            ....
        }
    }
}

Also note that once you do this, because in your json file country starts with - it will not get in

Sign up to request clarification or add additional context in comments.

Comments

1

Your file contains a FoodItemData[], not a FoodItemData (see also http://www.w3schools.com/json/json_syntax.asp and search for arrays). This array obviously misses all of the properties of the FoodItemData and the resulting object returns null for all properties. At the same time disabling FAIL_ON_UNKNOWN_PROPERTIES leads to no exceptions when trying to make a FoodItemData from it.

1 Comment

Can you explain a little bit more, I have been playing around with FoodItemData as an array and am still getting a null return value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.