我有一个特定的
JSON节点,对应于import org.codehaus.jackson.JsonNode,而不是
import org.codehaus.jackson.map.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更有效地执行此操作,但代码将会更加复杂.