我有一个特定的JSON节点,它对应于导入org.codehaus.jackson.JsonNode,而不是导入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" } ]
我想从上述数组的所有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来更有效地执行此操作,但是代码会更加混乱。