简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM