Home > OS >  Error when trying to convert from JSON Array to POJO
Error when trying to convert from JSON Array to POJO

Time:07-10

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)"

CodePudding user response:

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
  • Related