我喜欢这样的Json,我想读/读" responseCode"使用杰克逊,怎么做?
{
"successful":false,
"responseMessage":"direct address error: direct address is taken",
"responseCode":4610
}
答案 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();