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!
asked Mar 29, 2014 at 7:47
imakeitrayne
1651 gold badge2 silver badges11 bronze badges
-
What you have there is not valid JSON. It contains 2 JSON objects separated by a comma. So you can't parse it.JB Nizet– JB Nizet2014年03月29日 07:55:55 +00:00Commented Mar 29, 2014 at 7:55
-
How did you obtain the json?Bozho– Bozho2014年03月29日 07:57:16 +00:00Commented Mar 29, 2014 at 7:57
-
Oh, I received it from a friend who needed it parsed. I will try and add the {} for the whole file. First time using JSON and so I am not too bright on it.imakeitrayne– imakeitrayne2014年03月29日 16:11:50 +00:00Commented Mar 29, 2014 at 16:11
2 Answers 2
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
}
]
}
Beans:
public class Quotes {
private String file;
private String use;
private List<Phrases> famous_phrases;
public String getFile() {
return file;
}
public void setFile(String file) {
this.file = file;
}
public String getUse() {
return use;
}
public void setUse(String use) {
this.use = use;
}
public List<Phrases> getFamous_phrases() {
return famous_phrases;
}
public void setFamous_phrases(List<Phrases> famous_phrases) {
this.famous_phrases = famous_phrases;
}
}
public class Phrases {
private String phrase1;
private String phrase2;
private String id;
public String getPhrase1() {
return phrase1;
}
public void setPhrase1(String phrase1) {
this.phrase1 = phrase1;
}
public String getPhrase2() {
return phrase2;
}
public void setPhrase2(String phrase2) {
this.phrase2 = phrase2;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
read json file:
BufferedReader fileReader = new BufferedReader(new FileReader("./test.json"));
ObjectMapper mapper = new ObjectMapper();
Quotes quotes = mapper.readValue(fileReader, Quotes.class);
for (Phrases element : quotes.getFamous_phrases()) {
System.out.println(element.getPhrase1());
}
Sign up to request clarification or add additional context in comments.
1 Comment
imakeitrayne
Thanks, after fixing my json, this works a lot better.
You have to define a root node (wrap your json code with {})
{
"member1": {
"file": "phrases",
"use": "quotes"
},
"member2": {
"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
}
}]
}
answered Mar 29, 2014 at 7:55
drkunibar
1,3371 gold badge7 silver badges8 bronze badges
1 Comment
imakeitrayne
Thanks for the help, after seeing my json file was incorrect, this helps a lot as well.
lang-java