简体   繁体   English

使用 Jackson (JsonNode) 解析嵌套路径时遇到问题?

[英]Trouble using Jackson (JsonNode) to parse nested paths?

Trying to use Jackson to parse JSON.尝试使用 Jackson 来解析 JSON。 Simple enough- I'm clearly messing up something basic.很简单-我显然搞砸了一些基本的东西。

final JsonNode root = objectMapper.readTree(res.payload().asUtf8String());
JsonNode body = root.path("body");
JsonNode message = root.path("body").path("errorMessage");
logger.log(Level.INFO, body.asText());
logger.log(Level.INFO, message.asText());

Expected Result:预期结果:

INFO: {"errorMessage": "input is not a string"}
INFO: "input is not a string"

Actual Result:实际结果:

INFO: {"errorMessage": "input is not a string"}
INFO: 

Turns out it was because my nested value was, itself, escaped.原来这是因为我的嵌套值本身被转义了。

debugging root gave something of this form调试root给出了这种形式的东西

{
  status: 400,
  headers: {...},
  body: "{\"errorMessage\":\"input is not a string\"}"
}

The strategy then was to use the objectMapper again当时的策略是再次使用objectMapper

final JsonNode root = objectMapper.readTree(res.payload().asUtf8String());
// Body is, itself, escaped so we need to re-parse
JsonNode body = objectMapper.readTree(root.path("body").asText());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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