0

我有这个 json 对象,我很好奇如何遍历 servicecatalog:name 并警告任何不等于“service-foo”或“service-bar”的名称。

这是我的 json 对象:

{
    "access": {
        "serviceCatalog": [
            {
                "endpoints": [
                    {
                        "internalURL": "https://snet-storage101.example.com//v1.0", 
                        "publicURL": "https://storage101.example.com//v1.0", 
                        "region": "LON", 
                        "tenantId": "1
                    }, 
                    {
                        "internalURL": "https://snet-storage101.example.com//v1.0", 
                        "publicURL": "https://storage101.example.com//v1.0", 
                        "region": "USA", 
                        "tenantId": "1
                    }
                ], 
                "name": "service-foo", 
                "type": "object-store"
            }, 
            {
                "endpoints": [
                    {
                        "publicURL": "https://x.example.com:9384/v1.0/x", 
                        "tenantId": "6y5t4re32"
                    }
                ], 
                "name": "service-bar", 
                "type": "rax:test"
            }, 
            {
                "endpoints": [
                    {
                        "publicURL": "https://y.example.com:9384/v1.0/x", 
                        "tenantId": "765432"
                    }
                ], 
                "name": "service-thesystem", 
                "type": "rax:test"
            }
        ]
}
4

1 回答 1

0

如果 x 是上面提到的字典。你可以做

for item in x["access"]["serviceCatalog"]:
    if item["name"] not in ["service-foo", "service-bar"]:
        print(item["name"])

ps:如果您要求,您可以使用 json.loads() 来解码 json 数据。而且您的 JSON 中也有错误。

于 2012-11-03T06:56:16.103 回答