java – 用jackson删除JSON元素

前端之家收集整理的这篇文章主要介绍了java – 用jackson删除JSON元素前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个特定的 JSON节点,对应于import org.codehaus.jackson.JsonNode,而不是
import org.codehaus.jackson.map.JsonNode.
[
    {
        "givenName": "Jim","formattedName": "jimJackson","familyName": null,"middleName": "none","honorificPrefix": "mr","honorificSuffix": "none"
    },{
        "givenName": "john","formattedName": "johnLasher",{
        "givenName": "carlos","formattedName": "carlosAddner","honorifiPrefix": "mr",{
        "givenName": "lisa","formattedName": "lisaRay","honorificPrefix": "mrs",{
        "givenName": "bradshaw","formattedName": "bradshawLion",{
        "givenName": "phill","formattedName": "phillKane",{
        "givenName": "Gabriel","formattedName": "gabrielMoosa","honorificSuffix": "none"
    }
]

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

解决方法

我没有测试过这个,但我觉得这样做会做你想要的:
import org.codehaus.jackson.node.ObjectNode;
// ...
for (JsonNode personNode : rootNode) {
    if (personNode instanceof ObjectNode) {
        ObjectNode object = (ObjectNode) personNode;
        object.remove("familyName");
        object.remove("middleName");
    }
}

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

原文链接:https://www.f2er.com/java/121147.html

猜你在找的Java相关文章