杰克逊读作Intetger的价值

时间:2015-08-04 07:07:09

标签: jackson

我喜欢这样的Json,我想读/读" responseCode"使用杰克逊,怎么做?

{
"successful":false,
"responseMessage":"direct address error: direct address is taken",
"responseCode":4610
}

1 个答案:

答案 0 :(得分:0)

多种方式。一种是仅使用一个字段定义匹配的Java类:

// either mark to ignore anything else; or specify field/setter for every property, both work
@JsonIgnoreProperties(ignoreUnknown=true)
public class Response {
   public int responseCode;
}

Response resp = new ObjectMapper().readValue(json, Response.class);
int responseCode = resp.responseCode;

或使用树模型

JsonNode root = new ObjectMapper().readTree(json);
int responseCode = root.path("responseCode)".asIntValue();