Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error when trying to convert from JSON Array to POJO

I’m trying to create a POJO with the given Jackson String:

String json ={"name" : [{"John" , "Mark"}]};
ObjectMapper mapper = new ObjectMapper();
Students students = mapper.readValue(json, Students.class);

public class Students {
       String[] name;
      
       public Students (String[] name) (    
              this.name = name;
}

But I’m getting this error:

"No Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

try this

public static void main(String[] args) throws JsonProcessingException {
    String json = "{\"name\" : [\"John\" , \"Mark\"]}";
    ObjectMapper mapper = new ObjectMapper();
    Students students = mapper.readValue(json, Students.class);
}


public static class Students {
    private String[] name;

    public Students() {
    }

    public Students(String[] name) {
        this.name = name;
    }

    public String[] getName() {
        return name;
    }
}
  1. your json input is wrong : [{"John" , "Mark"}] -> ["John" , "Mark"]
  2. Add a default constructor
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading