0

So I came across this tutorial for serializing a POJO to json and then de-serialize the json file back to the POJO. http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/

he uses these helpful methods which worked for me but only for a single POJO in the file:

//1. Convert Java object to JSON format
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(new File("c:\\user.json"), user);

//2. Convert JSON to Java object
ObjectMapper mapper = new ObjectMapper();
User user = mapper.readValue(new File("c:\\user.json"), User.class);

How can I de-serialize a list of POJOs? My serialized file looks like the below:

[ {
  "name" : {
    "first" : "Wonder",
    "last" : "Woman"
  },
  "ssn" : "123-456-7890",
  "gender" : "FEMALE",
  "verified" : false
}, {
  "name" : {
    "first" : "Bat",
    "last" : "Man"
  },
  "ssn" : "321-456-0987",
  "gender" : "FEMALE",
  "verified" : true
}, {
  "name" : {
    "first" : "Super",
    "last" : "Man"
  },
  "ssn" : "321-654-1111",
  "gender" : "FEMALE",
  "verified" : true
} ]
Horse Voice
  • 8,138
  • 15
  • 69
  • 120
  • 1
    possible duplicate of [Jackson - Json to POJO With Multiple Entries](http://stackoverflow.com/questions/7045836/jackson-json-to-pojo-with-multiple-entries) – Luca Basso Ricci Aug 26 '13 at 20:49

3 Answers3

3
  1. One option (probably the easiest) is to define a class that contains a list of Users:

    public class Users
    {
      public User[] users;
    }
    

    Then performing

    ObjectMapper mapper = new ObjectMapper();
    Users users = mapper.readValue(new File("c:\\user.json"), Users.class);
    
  2. Another option would be to iterate over the json array, and capture each element of the array of users, then use ObjectMapper.readValue(String content, Class<T> valueType), like so:

    ObjectMapper mapper = new ObjectMapper();
    InputStream stream = new FileInputStream("c:\\user.json");
    User user;
    
    JsonNode json = mapper.readTree(stream);
    
    //NOTE: calling json.isArray() should return true.
    for (JsonNode userJson : json)
    {
      user = mapper.readValue(userJson, User.class);
      // use the constructed user...
    }
    

    Note: I haven't tested the above, so let me know if it works or not.

Luke Willis
  • 8,429
  • 4
  • 46
  • 79
  • Can you elaborate on option 2? Please provide a code example as I am new to Java. Thank you. – Horse Voice Aug 27 '13 at 12:35
  • @ImtiazAhmad elaborated. – Luke Willis Aug 27 '13 at 15:03
  • What is the InputStream from? Is it a jackson library or are you using java.io ? Also json.getElements() has a compilation error: `The method getElements() is undefined for the type JsonNode` – Horse Voice Aug 27 '13 at 17:47
  • @ImtiazAhmad Yes, the `InputStream` is out of `java.io`. I added a note, and removed the call to `getElements` entirely. – Luke Willis Aug 27 '13 at 17:56
  • `readValue()` is not compiling. It gives me the error: The method readValues(JsonParser, ResolvedType) in the type ObjectMapper is not applicable for the arguments (JsonNode, Class). Eclipse suggest, to change method call to `readValues()` but that doesnt work either. – Horse Voice Aug 27 '13 at 18:02
  • 1
    @ImtiazAhmad The API claims that [readValue(JsonNode, Class)](http://jackson.codehaus.org/1.7.9/javadoc/org/codehaus/jackson/map/ObjectMapper.html#readValue(org.codehaus.jackson.JsonNode,%20java.lang.Class)) exists, so I'm not sure why you're getting that. – Luke Willis Aug 27 '13 at 18:56
1

Hmmh? Have you tried:

User[] users = mapper.readValue(new File("c:\\user.json"), User[].class);
StaxMan
  • 113,358
  • 34
  • 211
  • 239
0

In your top level class, you have an array of people. something like this

    class People {
    public List<Person> persons;
}
scphantm
  • 4,293
  • 8
  • 43
  • 77