1

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!

3
  • What you have there is not valid JSON. It contains 2 JSON objects separated by a comma. So you can't parse it. Commented Mar 29, 2014 at 7:55
  • How did you obtain the json? Commented 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. Commented Mar 29, 2014 at 16:11

2 Answers 2

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

Thanks, after fixing my json, this works a lot better.
0

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

1 Comment

Thanks for the help, after seeing my json file was incorrect, this helps a lot as well.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.