1

我有这个字符串,我使用 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()如何检查 json 键是否存在?

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

但它总是向我显示“假”作为输出!

4

2 回答 2

4

如果您尝试查找放置在嵌套对象内的键,则可以使用 findValue(String key)在给定键未找到值时返回 null 的方法:

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
于 2020-06-22T15:37:21.263 回答
1

我认为您不受 has() 方法的约束。

您可以将json转换为地图,然后递归查找节点

    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 );
    }

递归方法应该访问所有节点并在找到节点后立即返回

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;
    }

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

于 2020-06-22T15:41:15.563 回答