检查 jsonNode 是否包含特定键,然后在它存在后提取其值

check if a jsonNode contains a specific key then EXTRACT its value once it exists

我有这个字符串,我使用 ObjectMapper 将它覆盖到 jsonNode。然后我试图在这个 jsonNode 中寻找一个特定的键,所以我使用了“.has”但是它没有用! 这是我所做的:

{ 
    "links": {
        "data": {
            "self": {
                "body": "", 
                "content_type": "",
                "href": "/api/v2/nodes",
                "method": "POST", 
                "name": "" 
            } 
        }
    },
    "results": { 
        "data": {
            "properties": { 
                "container": true,
                "container_size": 0,
                "create_date": "2020-06-22T16:19:07",
                "create_user_id": 1000,
                "description": ""
                "description_multilingual": { 
                    "en": "",
                    "fr_FR": "" 
                },
                "external_create_date": null,
                "external_identity": "",
                "external_identity_type": "",
                "external_modify_date": null,
                "external_source": "",
                "favorite": false,
                "id": 276625,
                "mime_type": null,
                "modify_date": "2020-06-22T16:19:07",
                "modify_user_id": 1000,
                "name": "mar",
                "name_multilingual": {
                    "en": "mar",
                    "fr_FR": ""
                },
                "owner": "Admin",
                "owner_group_id": 1001,
                "owner_user_id": 1000,
                "parent_id": 2000,
                "permissions_model": "advanced",
                "reserved": false,
                "reserved_date": null,
                "reserved_shared_collaboration": false,
                "reserved_user_id": 0,
                "size": 0,
                "size_formatted": "0 Eléments",
                "type": 0,
                "type_name": "Dossier",
                "versions_control_advanced": false,
                "volume_id": -2000 
            } 
        } 
    }
}

我想测试它是否有“id”键(它实际上存在于第31行)所以我使用了.has(),如How to check if a json key exists?:

ObjectMapper mapper = new ObjectMapper();    
JsonNode rootNode= mapper.readTree(body);
         if(rootNode.has("id")){
            System.out.println(true);
         }
        else{
            System.out.println(false);
        }

但它总是显示“false”作为输出!!

如果您要查找放置在嵌套对象中的键,您可以使用 findValue(String key) 方法 returns 如果给定键未找到值则为空:

ObjectMapper mapper = new ObjectMapper();
JsonNode rootNode= mapper.readTree(json);

String[] keys = {
    "id", "create_date", "versions_control_advanced", "name", "nofield"
};

for (String key : keys) {
    JsonNode value = rootNode.findValue(key);
    System.out.printf("Key %s exists? %s --> value=%s%n", key, value != null,
                    value == null ? null : value.asText());

}

输出:

Key id exists? true --> value=276625
Key create_date exists? true --> value=2020-06-22T16:19:07
Key versions_control_advanced exists? true --> value=false
Key name exists? true --> value=
Key nofield exists? false --> value=null

我认为你没有绑定到 has() 方法。

可以将json转为map,然后递归查找节点

    ObjectMapper mapper = new ObjectMapper();
    Map<String, Object> map = mapper.readValue( body, Map.class );

    ArrayList<Object> container = new ArrayList<>();
    boolean value = find( map, "id", container );

    if( value )
    {
        System.out.println( container );
    }

递归方法应该访问所有节点并且return一旦找到节点

private static boolean find( Map<String, Object> map, String search, ArrayList<Object> container )
    {
        int i = 0;
        for( String s : map.keySet() )
        {
            i++;
            if( s.equals( search ) )
            {
                container.add( map.get( s ) );
                return true;
            }
            if( map.get( s ) instanceof Map )
            {
                boolean found = find( (Map<String, Object>) map.get( s ), search, container );
                if( i == map.size() || found )
                {
                    return found;
                }
            }
        }
        return false;
    }

我也编辑了代码以获取该值。 希望这可以帮助。我强烈建议您在寻求社区帮助之前对自己进行更多研究。