- Given a list of user defined objects, we would liketo convert list of pojo objectsto JSON [and JSON to list of objects].
- We will use the jacksons objectmapper, to serialize list of objectsto JSON &deserialize JSON to List of objects.
- We will create Person class &we will perform following operations withPerson class.
- Convert List of Person objects to JSON
- Convert the JSONto List of Person objects.
- We have already discussed [similar transformations]:
- Convert Objects to/from JSON [jackson]
- Convert map to/from JSON [jackson]
1. Jackson objectMapper maven dependencies
com.fasterxml.jackson.core jackson-databind 2.7.12. Convert list of objects to/from JSON in java [jackson objectmapper]
2.1. Person Class:
- Person class containing attributesis as follows:
2.2. JSONListConverter Class:
JSONListConverter class is responsible for performing following operations.
- Convert List of Person objects to JSON String in java.
- Convert JSONString to List of Person objects in java.
Download Example Code Jackson List Object to JSON
3. Output list of objects to/from JSON in java [jackson objectmapper]
1. Convert List of person objects to JSON : [ { "firstName" : "Mike", "lastName" : "harvey", "age" : 34 }, { "firstName" : "Nick", "lastName" : "young", "age" : 75 }, { "firstName" : "Jack", "lastName" : "slater", "age" : 21 }, { "firstName" : "gary", "lastName" : "hudson", "age" : 55 } ] 2. Convert JSON to List of person objects : [Mike harvey 34] [Nick young 75] [Jack slater 21] [gary hudson 55]You may also like:
- Convert Map to/from JSON string in java [jackson objectmapper/ example]
- Convert Array of Objects to / from JSON Jackson ObjectMapper
- Convert object to/from json string in java [jackson objectmapper-example]
- Convert object having date to/from JSON in java [Jackson ObjectMapper example]
- Date serialization & deserialization POJO to JSON [GSON & example]