How do I remove empty json nodes in Java with Jackson?

前端 未结 3 1872
北海茫月
北海茫月 2021-01-14 16:25

I\'m a beginning java programmer, so I\'m sorry if my question is kind of dumb.

I have a JSON object that looks like this:

{
\"element1\" : {
    \"g         


        
3条回答
  •  滥情空心
    2021-01-14 17:03

    I haven't tested it, but you probably want something like this:

    public static void stripEmpty(JsonNode node) {
        Iterator it = node.iterator();
        while (it.hasNext()) {
            JsonNode child = it.next();
            if (child.isObject() && child.isEmpty(null))
                it.remove();
            else
                stripEmpty(child);
        }
    }
    

提交回复
热议问题