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

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

问题描述

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

我有一个Vendor对象,可以从单个vendorjson中正确反序列化,但我想将其反序列化为 Vendor [] ,I只是想不通如何让杰克逊合作。任何提示?

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?

推荐答案

您的数据存在问题,因为您的数组中有内部包装器对象。据推测,您的供应商对象旨在处理 id name company_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.

我假设你正在使用杰克逊 数据绑定 模型。

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

如果有,那么有两件事需要考虑:

If so then there are two things to consider:

第一种是使用特殊的Jackson配置属性。杰克逊 - 从1.9开始我认为,如果你使用旧版本的杰克逊,这可能无法使用 - 提供 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 。假设您有正确的供应商 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,
        ...
    ]

最后,您可以使用杰克逊 树模型 将其解析为 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.

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

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