imakeitrayne
imakeitrayne

Reputation: 165

Jackson json to objects

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!

Upvotes: 1

Views: 3741

Answers (2)

todd.tout
todd.tout

Reputation: 111

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

Upvotes: 2

drkunibar
drkunibar

Reputation: 1337

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

Upvotes: 0

Related Questions