Languages
[Edit]
EN

Java - deserialize JSON array to java object with Jackson lib (from json string to java object)

3 points
Created by:
ParaEagle
484

Quick solution:

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.Arrays;

public class JacksonDeserializeArray {

    public static void main(String[] args) throws JsonProcessingException {

        String usersJson = 
                "[{\"name\":\"Kate\",\"age\":24}," +
                "{\"name\":\"John\",\"age\":26}," +
                "{\"name\":\"Ann\",\"age\":25}]";

        ObjectMapper objectMapper = new ObjectMapper();
        User[] users = objectMapper.readValue(usersJson, User[].class);

        System.out.println(Arrays.toString(users));
    }

    private static class User {
        private String name;
        private int age;

        // Jackson requires to have this constructor
        public User() {

        }

        public User(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public int getAge() {
            return age;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            User user = (User) o;

            if (age != user.age) return false;
            return name != null ? name.equals(user.name) : user.name == null;
        }

        @Override
        public int hashCode() {
            int result = name != null ? name.hashCode() : 0;
            result = 31 * result + age;
            return result;
        }

        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
}

 

Create users json string from java User objects:

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.List;

public class ConvertUsersToJson {

    public static void main(String[] args) throws JsonProcessingException {
        List<User> users = new ArrayList<>();
        users.add(new User("Kate", 24));
        users.add(new User("John", 26));
        users.add(new User("Ann", 25));

        ObjectMapper objectMapper = new ObjectMapper();
        String usersJson = objectMapper.writeValueAsString(users);

        // [{"name":"Kate","age":24},{"name":"John","age":26},{"name":"Ann","age":25}]
        System.out.println(usersJson);
    }

    private static class User {
        private String name;
        private int age;

        // Jackson requires to have this constructor
        public User() {

        }

        public User(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public int getAge() {
            return age;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            User user = (User) o;

            if (age != user.age) return false;
            return name != null ? name.equals(user.name) : user.name == null;
        }

        @Override
        public int hashCode() {
            int result = name != null ? name.hashCode() : 0;
            result = 31 * result + age;
            return result;
        }

        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
}

 

Java JSON - Jackson lib

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join