-1
 {
    "response": [
        {
            "id": "1",
            "name": "xx"
        },
        {
            "id": "2",
            "name": "yy"
        }
    ],
    "errorMsg": "",
    "code": 0
}

How to parse "response" alone using jackson parser. I am getting error as

Unrecognized field "errorMsg", not marked as ignorable.

My model class Response.java

public class Response {
@JsonProperty("id")
private Integer id;
@JsonProperty("name")
private String name;
}
Runcorn
  • 5,144
  • 5
  • 34
  • 52
Ria
  • 873
  • 1
  • 10
  • 25
  • possible duplicate of [Jackson with JSON: Unrecognized field, not marked as ignorable](http://stackoverflow.com/questions/4486787/jackson-with-json-unrecognized-field-not-marked-as-ignorable) – cy3er Aug 05 '14 at 10:59

2 Answers2

2

Your data model is a bit incomplete and this is what Jackson is pointing out. To improve the situation you should map more fields.

public class Response {
    @JsonProperty("id")
    private Integer id;
    @JsonProperty("name")
    private String name;
    // getter/setter...
}
public class Data {
    @JsonProperty("response")
    private List<Response> response;
    @JsonProperty("errorMsg")
    private String errorMsg;
    @JsonProperty("code")
    private int code;
    // getter/setter...
}
FreakyBytes
  • 151
  • 1
  • 9
0

You can either create a parent object and use @JsonIgnoreProperties. Alternatievly you could get the node and convert it to response object using ObjectMapper's convertValue() method like

try {
    String json = "{\"response\":[{\"id\":\"1\",\"name\":\"xx\"},{\"id\":\"2\",\"name\":\"yy\"}],\"errorMsg\":\"\",\"code\":0}";
    ObjectMapper mapper = new ObjectMapper();
    JsonNode node = mapper.readTree(json);
    List<Response> responses = mapper.convertValue(node.findValues("response").get(0), new TypeReference<List<Response>>() {});
    System.out.println(responses);
} catch (JsonProcessingException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
Syam S
  • 8,421
  • 1
  • 26
  • 36