Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert the following JSON String to POJO

Tags:

java

json

jackson

I want to convert the following JSON string to a Java object:

{
  "user": {
    "0": {
      "firstName": "Monica",
      "lastName": "Belluci"
    },
    "1": {
      "firstName": "John",
      "lastName": "Smith"
    },
    "2": {
      "firstName": "Owen",
      "lastName": "Hargreaves"
    }
  }
}

To convert this to Java object I've created the following classes:

class User {
    private Map<String, MyObject> user = new HashMap<>();
    //Getter and Setter is here
}

class MyObject {
    private String firstName;
    private String lastName;
    //Getters and Setters are here
}

I'm using Jackson library to convert JSON to Java. Here is how I'm using the Jackson for conversion:

ObjectMapper mapper = new ObjectMapper();
User user = mapper.readValue(jsonString, User.class);

The problem is that with this conversion above the Map inside the User object is always empty. What am I doing wrong?

Thanks in advance.

like image 613
Arsen Davtyan Avatar asked Jan 06 '17 06:01

Arsen Davtyan


2 Answers

I think it should work. I've executed this code and it works fine. Here is my example.

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;

public class TestJackson {

public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        String testJson = "{\n" + "  \"user\": {\n" + "    \"0\": {\n" + "      \"firstName\": \"Monica\",\n" + "      \"lastName\": \"Belluci\"\n" + "    },\n" + "    \"1\": {\n" + "      \"firstName\": \"John\",\n" + "      \"lastName\": \"Smith\"\n" + "    },\n" + "    \"2\": {\n" + "      \"firstName\": \"Owen\",\n" + "      \"lastName\": \"Hargreaves\"\n" + "    }\n" + "  }\n" + "}";
        User readValue = mapper.readValue(testJson, User.class);
        System.out.println("readValue = " + readValue);
    }
}

and the User.class:

import java.util.HashMap;
import java.util.Map;

class User {
    private Map<String, MyObject> user = new HashMap<String, MyObject>();

    public Map<String, MyObject> getUser() {
        return user;
    }

    public void setUser(Map<String, MyObject> user) {
        this.user = user;
    }

    @Override
    public String toString() {
        return "User{" +
                "user=" + user +
                '}';
    }
}

class MyObject {
    private String firstName;
    private String lastName;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return "MyObject{" +
                "firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                '}';
    }
}
like image 162
K. Gol Avatar answered Oct 22 '22 01:10

K. Gol


I had one additional issue, that i faced when converting JSON to Java POJO: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of class out of START_ARRAY token ...

If anyone faces this issue, this is because JSON is expecting an object {} but it sees an array [{}] within the JSON String that is passed in

ObjectMapper mapper = new ObjectMapper();
User user = mapper.readValue(jsonString, User.class);

To fix

User[] user = mapper.readValue(jsonString, User[].class);

Reference: https://stackoverflow.com/a/33515796/4167786

like image 22
she12 Avatar answered Oct 22 '22 00:10

she12