如何使用 Jackson 在 Java 中删除空的 json 节点?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19159596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 14:36:08  来源:igfitidea点击:

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

javajsonHymanson

提问by user2842643

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

我是一个初学 Java 程序员,所以如果我的问题有点愚蠢,我很抱歉。

I have a JSON object that looks like this:

我有一个如下所示的 JSON 对象:

{
"element1" : {
    "generated_name_1": {
        "a" : {"isReady":false}
    },
    "generated_name_2":{},
    "generated_name_3":{},
    "generated_name_4":{}
},
"element2" : {
    "generated_name_5" : {
        "a" : {"isReady":false},
        "g" : {"isReady":false}
    }
},
"element3" : { 
    "a" : { "isReady":false},
    "n":{}
}
}

I would like to go through and delete every element that has an empty value associated with it, like "generated_name_2" and "n". I have no idea what the names of those elements would be, and I have no idea how far nested into the JSON tree it is.

我想遍历并删除每个具有与之关联的空值的元素,例如“generated_name_2”和“n”。我不知道这些元素的名称是什么,也不知道它在 JSON 树中嵌套了多远。

I get that I have to write a recursive program, and this is what I came up with:

我知道我必须编写一个递归程序,这就是我想出的:

public static void cleanJsonNodes(ObjectNode myJsonNode){
    for (JsonNode currentNode : myJsonNode){
        if (currentNode.size() == 0){
            myJsonNode.remove(currentNode);
        } else {
            cleanJsonNodes((ObjectNode)currentNode);
        }
    }
}

Of course, this doesn't work, but I'm not really sure where to go from here and I've scoured the internet to no avail.

当然,这行不通,但我不确定从哪里开始,我已经在互联网上搜索无济于事。

Please someone help me!

请有人帮助我!

回答by Maxim Shoustin

I want only to show direction how to do that with Json:

我只想说明如何使用 Json 做到这一点:

JSONObject jsonObj = new JSONObject(_message);

    Map<String, JSONObject> map = jsonObj.getMap();

    Iterator<String> it = map.keySet().iterator();

    while(it.hasNext()){
          String key =   it.next();

          JSONObject o = map.get(key);

          if(o.length() == 0){
              it.remove();
          }
    }

When JSONObjectloads {}its length is 0, therefore you can drop it.

JSONObject加载时,{}其长度为 0,因此您可以删除它。

As A side note, you can use this method in recursion like:

作为旁注,您可以在递归中使用此方法,例如:

JSONObject jsonObj = new JSONObject(_message);

invoke(jsonObj);

...

private static void invoke(JSONObject jsonObj) {
    Map<String, JSONObject> map = jsonObj.getMap();

    Iterator<String> it = map.keySet().iterator();

    while(it.hasNext()){
        String key =   it.next();

        JSONObject o = map.get(key);

        if(o.length() == 0){
            it.remove();
        }
        else{
            invoke(o);
        }
    }
}

I didn't add any validation but for sure , you need to verify instance of jsonObj.getMap()...

我没有添加任何验证,但可以肯定的是,您需要验证jsonObj.getMap()...的实例

回答by Neil

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

我还没有测试过,但你可能想要这样的东西:

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