我正在从一个多嵌套的json文件中读取.我已经创建了一个用于从json获取特定密钥的类,然后将其创建到一个数据帧中.

现在,如果密钥存在,则一切都按预期工作,并且找不到密钥时,我会得到以下错误

TypeError:‘NoneType’对象不可订阅

下面是一个样例json和代码快照

output = {"records":[{
    "ID":{
        "$":"123456"
    },
    "Records":{
        "ChildRecord":{
            "Address":[
                {
                    "$": "24 Street"
                }
            ]
        }
    }
},{
    "ID":{
        "$":"456789"
    },
    "Records":{
        "ChildRecord":{
            "Address":[
                {
                    "$": "57 New Town"
                }
            ]
        }
    }
}]}

identifier = [o.get('ID').get('$') for o in output['records']] 
address = [o.get('Records').get('ChildRecord').get('Address')[0].get('$') for o in output['records']]
print(identifier,'identifier')
print(address,'address')

由于密钥存在,因此我得到以下输出

identifier ['123456','456789'] 
address ['24 Street','57 New Town']

If the "Address" key is not present in one of the record then I get the type error, TypeError:‘NoneType’对象不可订阅. How to overcome this issue and get the output as

identifier ['123456','456789'] 
address ['24 Street',None]

此外,在某些情况下,我会在多个密钥中获取地址

"Address":[
                    {
                        "$": "57 New Town"
                    },
                    {   
                        "$": "New Castle Road"
                    },
                    {   
                        "$": "PO Box 309"
                    }
                ]

有没有办法将整个地址读作

['24 Street','57 New Town, New Castle Road, PO Box 309']

推荐答案

问题出在这一部分:.get('Address')[0]-当Address不存在时,.get()返回None并引发异常.

下面是略微修改的版本,如果Address不存在(并且不抛出错误),则返回None.OR连接多个地址(如果它们存在):

output = {
    "records": [
        {
            "ID": {"$": "123456"},
            "Records": {"ChildRecord": {"Address": [{"$": "24 Street"}]}},
        },
        {
            "ID": {"$": "456789"},
            "Records": {"ChildRecord": {}},
        },
    ]
}


identifier = [o.get("ID").get("$") for o in output["records"]]
address = [
    ", ".join(
        d["$"] for d in o.get("Records", {}).get("ChildRecord", {}).get("Address", [])
    )
    or None
    for o in output["records"]
]
print(identifier, "identifier")
print(address, "address")

打印:

['123456', '456789'] identifier
['24 Street', None] address

它是output

output = {
    "records": [
        {
            "ID": {"$": "123456"},
            "Records": {"ChildRecord": {"Address": [{"$": "24 Street"}]}},
        },
        {
            "ID": {"$": "456789"},
            "Records": {
                "ChildRecord": {
                    "Address": [{"$": "57 New Town"}, {"$": "New Castle Road"}]
                }
            },
        },
    ]
}

然后打印代码:

['123456', '456789'] identifier
['24 Street', '57 New Town, New Castle Road'] address

Json相关问答推荐

Vega通孔信号中的动态梯度

PowerShell:将Invoke-WebRequest与变量一起使用

为什么JQ筛选器不将原始输入打印为$var|.';文档?

如何在Haskell中解析JSON,其中字段的名称可以是多个值之一,但应该转换为单个Haskell类型?

Jolt-在数组列表中插入新的字段

如何避免解析 ISuperObject 类型字段中的 json 对象

如何使用jolt规范将一个对象添加到另一个对象中并删除该对象

APIM 生成 JsonArray 到 EventHub

VBA-JSON 嵌套集合解析为 Excel

颠簸转换问题

Servicestack 返回数组而不是带有数组的对象

使用 Javascript 判断 JSON 对象是否包含值

如何在 Perl 中将简单的哈希转换为 json?

如何使用 Newtonsoft.Json 包在 C#(4.0) 中解析我的 json 字符串?

如何转换为 D3 的 JSON 格式?

有什么方法可以判断您安装的 gulp 版本吗?

有没有办法折叠 Postman 中的所有 json 字段

XML vs YAML vs JSON

在播放框架 JsObject 中解析 Json 数组

log4j 支持 JSON 格式吗?