I can't for the life of me figure out how to parse this json file into objects using jackson.
Here is my json file:
{
"file": "phrases",
"use": "quotes",
"famous_phrases": [
{
"phrase1": "one for all",
"phrase2": "all for one",
"id": 1
},
{
"phrase1": "four scores",
"phrase2": "and seven years ago",
"id": 17
},
{
"phrase1": "elementary",
"phrase2": "my dear watson",
"id": 22
}
]
}
I tried this:
BufferedReader fileReader = new BufferedReader(new FileReader("./test.json"));
ObjectMapper mapper = new ObjectMapper();
JsonNode quotes = mapper.readValue(fileReader, JsonNode.class);
quotes = quotes.get("famous_phrases");
TypeReference<List<Quotes>> phrases = new TypeReference<List<Quotes>>(){};
List<Quotes> q = mapper.readValue(quotes.traverse(), phrases);
for (Phrases element : q) {
System.out.println(element.getPhrase1());
}
With a POJO I made but I think I might have made the POJO incorrectly. I defined all the attributes (file, use, famous_phrases) and each one had its own set and get methods. Any help into this would be appreciated!