【问题标题】:Jackson - unwrapping object杰克逊 - 展开对象
【发布时间】:2013-10-05 15:21:34
【问题描述】:

我收到来自服务器的响应,其中包含一些附加信息。例如:

{ “response_a”:..., “一些元数据”:1234, “更多元数据”:abcd } 要么 { “response_b”:[...], “一些元数据”:1234, “更多元数据”:abcd }

“response_x”可以是自定义对象、列表或哈希图,根据请求可以有不同的名称。

有没有办法只反序列化 response_x,或者使用 jackson 将其作为字符串获取?

【问题讨论】:

  • 反序列化过程中是否有这个“response_x”属性名?
  • 是的,我愿意。我想我可以简单地使用JSONObject.get("response_x").toString(),但我非常想用杰克逊来做到这一点......

标签: java android json serialization jackson


【解决方案1】:

您可以将上面的 JSON 反序列化为 Map 并使用 get 方法检索属性:

ObjectMapper mapper = new ObjectMapper();
MapType mapType = mapper.getTypeFactory().constructMapType(HashMap.class, String.class, Object.class);
Map<String, Object> result = mapper.readValue(json, mapType);
Object responseX = result.get("response_x");

【讨论】:

    猜你喜欢
    • 2017-08-04
    • 2017-10-03
    • 2018-06-21
    • 1970-01-01
    • 2022-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多