Jackson - 如何处理(反序列化)嵌套的 JSON? [英] Jackson - How to process (deserialize) nested JSON?

查看:108
本文介绍了Jackson - 如何处理(反序列化)嵌套的 JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

{
  vendors: [
    {
      vendor: {
        id: 367,
        name: "Kuhn-Pollich",
        company_id: 1,
      }
    },
    {
      vendor: {
        id: 374,
        name: "Sawayn-Hermann",
        company_id: 1,
      }
  }]
}

我有一个可以从单个供应商"json 正确反序列化的供应商对象,但我想将其反序列化为 供应商 [],我只是不知道如何制作杰克逊合作.有什么提示吗?

I have a Vendor object that can properly be deserialized from a single "vendor" json, but I want to deserialize this into a Vendor[], I just can't figure out how to make Jackson cooperate. Any tips?

推荐答案

您的数据有问题,因为您的数组中有内部 wrapper 对象.据推测,您的 Vendor 对象旨在处理 idnamecompany_id,但这些多个对象中的每一个也是包裹在一个具有单一属性 vendor 的对象中.

Your data is problematic in that you have inner wrapper objects in your array. Presumably your Vendor object is designed to handle id, name, company_id, but each of those multiple objects are also wrapped in an object with a single property vendor.

我假设您正在使用 Jackson 数据绑定 模型.

I'm assuming that you're using the Jackson Data Binding model.

如果是这样,那么需要考虑两件事:

If so then there are two things to consider:

第一个是使用特殊的 Jackson 配置属性.Jackson - 我相信从 1.9 开始,如果您使用的是旧版本的 Jackson,这可能不可用 - 提供 UNWRAP_ROOT_VALUE.它专为您的结果包含在您想要丢弃的顶级单一属性对象中的情况而设计.

The first is using a special Jackson config property. Jackson - since 1.9 I believe, this may not be available if you're using an old version of Jackson - provides UNWRAP_ROOT_VALUE. It's designed for cases where your results are wrapped in a top-level single-property object that you want to discard.

所以,玩玩:

objectMapper.configure(SerializationConfig.Feature.UNWRAP_ROOT_VALUE, true);

第二种是使用包装对象.即使在丢弃外部包装对象之后,您仍然存在 Vendor 对象被包装在单一属性对象中的问题.使用包装器来解决这个问题:

The second is using wrapper objects. Even after discarding the outer wrapper object you still have the problem of your Vendor objects being wrapped in a single-property object. Use a wrapper to get around this:

class VendorWrapper
{
    Vendor vendor;

    // gettors, settors for vendor if you need them
}

同样,您也可以定义一个包装类来处理外部对象,而不是使用 UNWRAP_ROOT_VALUES.假设你有正确的 Vendor, VendorWrapper 对象,你可以定义:

Similarly, instead of using UNWRAP_ROOT_VALUES, you could also define a wrapper class to handle the outer object. Assuming that you have correct Vendor, VendorWrapper object, you can define:

class VendorsWrapper
{
    List<VendorWrapper> vendors = new ArrayList<VendorWrapper>();

    // gettors, settors for vendors if you need them
}

// in your deserialization code:
ObjectMapper mapper = new ObjectMapper();
JsonNode rootNode = mapper.readValue(jsonInput, VendorsWrapper.class); 

VendorsWrapper 的对象树类似于您的 JSON:

The object tree for VendorsWrapper is analogous to your JSON:

VendorsWrapper:
    vendors:
    [
        VendorWrapper
            vendor: Vendor,
        VendorWrapper:
            vendor: Vendor,
        ...
    ]

最后,您可以使用 Jackson 树模型 将其解析为 JsonNodes,丢弃外层节点,对于ArrayNode中的每个JsonNode,调用:

Finally, you might use the Jackson Tree Model to parse this into JsonNodes, discarding the outer node, and for each JsonNode in the ArrayNode, calling:

mapper.readValue(node.get("vendor").getTextValue(), Vendor.class);

这可能会导致更少的代码,但它似乎并不比使用两个包装器更笨拙.

That might result in less code, but it seems no less clumsy than using two wrappers.

这篇关于Jackson - 如何处理(反序列化)嵌套的 JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆