Let's say I want to directly read the object fruits from a Json file and store it to the Fruit.class. How should I do it?
{
"fruits": [
{ "name":"Apple", "price":"0.8"}
],
"vegetables": [
{ "name":"Carrot", "price":"0.4"}
]
}
My model class
public class Fruit {
private String name;
private double price;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public double getPrice() { return price; }
public void setPrice(double price) { this.price= price; }
}
What I've tried:
String file = "src/main/resources/basket.json";
String json = new String(Files.readAllBytes(Paths.get(file)));
objectMapper.readValue(json, Fruit.class);