Python 访问JSON对象中的嵌套键

Python 访问JSON对象中的嵌套键,python,json,Python,Json,我想在Python中访问一个特定的键值(“cote_1989_base”)来修改它。但我有一个类型错误:字符串索引必须是整数 我的代码: 导入json 以open('demo_db_algolia_2019.json',encoding='utf-8')作为数据文件: data=json.load(data\u file.read()) 对于数据中的x: cote1989base=x.get([“cote”][0][“cote_1989”][“cote_1989_eu”][“cote_1989_

我想在Python中访问一个特定的键值(“cote_1989_base”)来修改它。但我有一个类型错误:字符串索引必须是整数

我的代码:

导入json
以open('demo_db_algolia_2019.json',encoding='utf-8')作为数据文件:
data=json.load(data\u file.read())
对于数据中的x:
cote1989base=x.get([“cote”][0][“cote_1989”][“cote_1989_eu”][“cote_1989_base”])
打印(cote1989base)
编辑:也许我没有解释清楚,因为我的整个JSON都在这样一个数组中:

    [{
    "objectID": 10035,
    "cote":
    {
        "cote_1989":
        {
            "cote_1989_f": "750000F",
            "cote_1989_eu":
            {                    
                "cote_1989_base": 190140                    
            }
        },
        "cote_2004":
        {                
            "cote_2004_base": 173320                
        },
        "cote_2014":
        {                
            "cote_2014_base": 420800                
        },
        "cote_2017":
        {                
            "cote_2017_base": 939600                
        },
        "cote_2019":
        {                
            "cote_2019_base": 939600                
        }
    }
},
    {
    "objectID": 10202,
    "cote":
    {
        "cote_1989":
        {
            "cote_1989_f": "27000F",
            "cote_1989_eu":
            {                    
                "cote_1989_base": 6844                    
            }
        },
        "cote_2004":
        {
            "cote_2004_base": 10894                
        },
        "cote_2014":
        {
            "cote_2014_base": 23670
        },
        "cote_2017":
        {                
            "cote_2017_base": 46980
        },
        "cote_2019":
        {                
            "cote_2019_base": 51156
        }
    }
}
]

它是否改变了主要问题?

您应该按以下方式访问密钥:

x.get("cote").get("cote_1989").get("cote_1989_eu").get("cote_1989_base")
也可以使用以下函数简化代码:

def find(data, key):
    parts = key.split('/')
    dd = data
    for part in parts:
         if ( dd == None or not isinstance(dd,dict)):
            return None
         dd = dd.get(part)         
    return dd    


您应按以下方式访问密钥:

x.get("cote").get("cote_1989").get("cote_1989_eu").get("cote_1989_base")
也可以使用以下函数简化代码:

def find(data, key):
    parts = key.split('/')
    dd = data
    for part in parts:
         if ( dd == None or not isinstance(dd,dict)):
            return None
         dd = dd.get(part)         
    return dd    

这一位:
x.get([“cote”][0][“cote_1989”]
肯定是错误的。这里的方括号不是
dict
查找,它是一个列表文字,因此
[“cote”][0]
计算结果是
cote

然后你继续用
[“cote_1989”]
给它下标,这就是你的
类型错误的来源

您应该链接
.get()
调用:

x.get("cote").get("cote_1989").get("cote_1989_eu").get("cote_1989_base")
假设您的原始JSON是正确的,那么将
[0]
位视为原始代码中的“行噪声”。此位:
x.get([“cote”][0][“cote_1989”]
肯定是错误的。这里的方括号不是
dict
查找,而是列表文字,因此
[“cote”][0]
的计算结果是
“cote”

然后你继续用
[“cote_1989”]
给它下标,这就是你的
类型错误的来源

您应该链接
.get()
调用:

x.get("cote").get("cote_1989").get("cote_1989_eu").get("cote_1989_base")

假设您的原始JSON是正确的,则将
[0]
位视为原始代码中的“行噪声”。

JSON
中存在错误。大括号不匹配。请使用此选项

import json


j = '''
{
    "objectID": 10035,
    "cote": {
        "cote_1989": {
            "cote_1989_f": "750000F",
            "cote_1989_eu": {
                "cote_1989_excp": 266196,
                "cote_1989_concours": 228168,
                "cote_1989_base": 190140,
                "cote_1989_be": 152112,
                "cote_1989_me": 114084,
                "cote_1989_ar": 76056,
                "cote_1989_epa": 38028
            }
        },
        "cote_2004": {
            "cote_2004_excp": 242648,
            "cote_2004_concours": 207984,
            "cote_2004_base": 173320,
            "cote_2004_be": 138656,
            "cote_2004_me": 103992,
            "cote_2004_ar": 69328,
            "cote_2004_epa": 34664
        }
    }
}'''

data = json.loads(j)
print(data['cote']['cote_1989']['cote_1989_eu']['cote_1989_base'])
输出:

190140

JSON
中存在错误。大括号不匹配。请使用此选项

import json


j = '''
{
    "objectID": 10035,
    "cote": {
        "cote_1989": {
            "cote_1989_f": "750000F",
            "cote_1989_eu": {
                "cote_1989_excp": 266196,
                "cote_1989_concours": 228168,
                "cote_1989_base": 190140,
                "cote_1989_be": 152112,
                "cote_1989_me": 114084,
                "cote_1989_ar": 76056,
                "cote_1989_epa": 38028
            }
        },
        "cote_2004": {
            "cote_2004_excp": 242648,
            "cote_2004_concours": 207984,
            "cote_2004_base": 173320,
            "cote_2004_be": 138656,
            "cote_2004_me": 103992,
            "cote_2004_ar": 69328,
            "cote_2004_epa": 34664
        }
    }
}'''

data = json.loads(j)
print(data['cote']['cote_1989']['cote_1989_eu']['cote_1989_base'])
输出:

190140

您拥有的不是有效的json,也请执行
type(data)
检查
数据的类型
我编辑了json。它只是我数据库的一个示例,但在我测试它时它是有效的。该代码应该会引发
AttributeError:'str'对象没有属性“get”
,这正是它在这里所做的。好吧,
[“cote”][0]
是一个字符串,因此
[“cote”][0][“cote_1989”]
毫无意义。您还希望看到什么?您所拥有的不是有效的json,也需要
类型(数据)
为了检查
数据的类型
我编辑了JSON。它只是我数据库的一个样本,但在我测试它时是有效的。该代码应该会引发
属性错误:“str”对象没有属性“get”
,这正是它在这里所做的。好吧,
[“cote”][0]
是一个字符串,所以
[“cote”][0][“cote\u 1989”]
根本没有意义。你还期望什么?我有一个AttributeError:“非类型”对象没有属性“get”,可能是因为有时候,“cote_1989”键可能没有值。你可以使用上面的函数来简化代码。我有一个AttributeError:“非类型”对象没有属性“get”,可能是因为有时候“cote_1989”键可能没有值。您可以使用上述函数简化代码。将行
data=json.loads(j)
更改为
data=json.loads(data_file.read())
并确保
JSON
没有错误。如果可能,使用
vscode
检查JSON文件。使用vscode打开
.JSON
文件,它将显示文件是否有错误。将行
data=JSON.loads(j)
更改为
data=JSON.loads(data_file.read())
并确保
JSON
没有错误。如果可能,使用
vscode
检查JSON文件。使用vscode打开
.JSON
文件,它将显示文件是否有错误。