本文介绍了使用 jackson 删除 JSON 元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对应于 import org.codehaus.jackson.JsonNode 的特定 JSON 节点,而不是导入 org.codehaus.jackson.map.JsonNode.

[{"givenName": "吉姆","formattedName": "jimJackson",家庭名称":空,"中间名": "无","honorificPrefix": "先生","honorificSuffix": "无"},{"givenName": "约翰","formattedName": "johnLasher",家庭名称":空,"中间名": "无","honorificPrefix": "先生","honorificSuffix": "无"},{"givenName": "卡洛斯","formattedName": "carlosAddner",家庭名称":空,"中间名": "无","honorifiPrefix": "先生","honorificSuffix": "无"},{"givenName": "丽莎","formattedName": "lisaRay",家庭名称":空,"中间名": "无","honorificPrefix": "夫人","honorificSuffix": "无"},{"givenName": "布拉德肖","formattedName": "bradshawLion",家庭名称":空,"中间名": "无","honorificPrefix": "先生","honorificSuffix": "无"},{"givenName": "phil","formattedName": "phillKane",家庭名称":空,"中间名": "无","honorificPrefix": "先生","honorificSuffix": "无"},{"givenName": "加布里埃尔","formattedName": "gabrielMoosa",家庭名称":空,"中间名": "无","honorificPrefix": "先生","honorificSuffix": "无"}]

我想从上述数组的所有 JSON 节点中删除familyName"和middleName".有没有办法实现这一目标?

解决方案

我还没有测试过这个,但我认为这样的事情会做你想要的:

import org.codehaus.jackson.node.ObjectNode;//...for (JsonNode personNode : rootNode) {if (personNode instanceof ObjectNode) {ObjectNode 对象 = (ObjectNode) personNode;object.remove("familyName");object.remove("middleName");}}

您还可以使用 Jackon 的原始解析 API 更有效地执行此操作,但代码会更加混乱.

I've a particular JSON Node that corresponds to import org.codehaus.jackson.JsonNode, and notimport org.codehaus.jackson.map.JsonNode.

[
    {
        "givenName": "Jim",
        "formattedName": "jimJackson",
        "familyName": null,
        "middleName": "none",
        "honorificPrefix": "mr",
        "honorificSuffix": "none"
    },
    {
        "givenName": "john",
        "formattedName": "johnLasher",
        "familyName": null,
        "middleName": "none",
        "honorificPrefix": "mr",
        "honorificSuffix": "none"
    },
    {
        "givenName": "carlos",
        "formattedName": "carlosAddner",
        "familyName": null,
        "middleName": "none",
        "honorifiPrefix": "mr",
        "honorificSuffix": "none"
    },
    {
        "givenName": "lisa",
        "formattedName": "lisaRay",
        "familyName": null,
        "middleName": "none",
        "honorificPrefix": "mrs",
        "honorificSuffix": "none"
    },
    {
        "givenName": "bradshaw",
        "formattedName": "bradshawLion",
        "familyName": null,
        "middleName": "none",
        "honorificPrefix": "mr",
        "honorificSuffix": "none"
    },
    {
        "givenName": "phill",
        "formattedName": "phillKane",
        "familyName": null,
        "middleName": "none",
        "honorificPrefix": "mr",
        "honorificSuffix": "none"
    },
    {
        "givenName": "Gabriel",
        "formattedName": "gabrielMoosa",
        "familyName": null,
        "middleName": "none",
        "honorificPrefix": "mr",
        "honorificSuffix": "none"
    }
]

I want to remove the "familyName" and "middleName" from all the JSON nodes of the above array. Is there any way to achieve this?

解决方案

I haven't tested this, but I think something like this would do what you want:

import org.codehaus.jackson.node.ObjectNode;
// ...
for (JsonNode personNode : rootNode) {
    if (personNode instanceof ObjectNode) {
        ObjectNode object = (ObjectNode) personNode;
        object.remove("familyName");
        object.remove("middleName");
    }
}

You could also do this more efficiently using Jackon's raw parsing API, but the code would be a lot messier.

这篇关于使用 jackson 删除 JSON 元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 13:39