Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中解析JSON时获取Keyerror_Python_Json_String_Parsing - Fatal编程技术网

在Python中解析JSON时获取Keyerror

在Python中解析JSON时获取Keyerror,python,json,string,parsing,Python,Json,String,Parsing,我刚刚制作了一个程序来解析api中的一些数据。api以JSON格式返回数据。当我试图解析它时,它会给我一个关键错误 url = json.loads(r.text)["url"] KeyError: 'url' 这是代码的一部分 url = json.loads(r.text)["url"] 我正在尝试获取普通字段中的数据。以下是API的输出: {"updates":[{"id":"a6aa-8bd","description":"Bug fixes and enhancem

我刚刚制作了一个程序来解析api中的一些数据。api以JSON格式返回数据。当我试图解析它时,它会给我一个关键错误

    url = json.loads(r.text)["url"]
    KeyError: 'url'
这是代码的一部分

url = json.loads(r.text)["url"]
我正在尝试获取普通字段中的数据。以下是API的输出:

{"updates":[{"id":"a6aa-8bd","description":"Bug fixes and enhancemets","version":"8.1.30","type":"firmware","url":"https://con-man.company.com/api/v1/file-732e844b","updated":"2017-07-25"}]}
试试这个

url = json.loads(r.text)["updates"][0]["url"]
试试这个

url = json.loads(r.text)["updates"][0]["url"]

您无法访问
url
,因为它在更新(列表)中,所以您需要先传递索引,然后传递

一艘班轮:

>>> url = json.loads(r.text)['updates'][0]['url']
'https://con-man.company.com/api/v1/file-732e844b'
明确的

>>> jobj = json.loads(r.text)
>>> url = jobj['updates'][0]['url']
'https://con-man.company.com/api/v1/file-732e844b'

您无法访问
url
,因为它在更新(列表)中,所以您需要先传递索引,然后传递

一艘班轮:

>>> url = json.loads(r.text)['updates'][0]['url']
'https://con-man.company.com/api/v1/file-732e844b'
明确的

>>> jobj = json.loads(r.text)
>>> url = jobj['updates'][0]['url']
'https://con-man.company.com/api/v1/file-732e844b'
试着想象一下你的dict,它只有一个键“update”,在这个键值中有另一个列表,在这个列表中,你有另一个dict

那么如果你的情况是这样的话

 _dict = json.loads(r.text)   # read file and load dict
 _list = _dict['updates']     # read list inside dict
 _dict_1 = _list[0]       # read list first value and load dict 
 url = _dict_1['url']     # read 'url' key from dict 
试着想象一下你的dict,它只有一个键“update”,在这个键值中有另一个列表,在这个列表中,你有另一个dict

那么如果你的情况是这样的话

 _dict = json.loads(r.text)   # read file and load dict
 _list = _dict['updates']     # read list inside dict
 _dict_1 = _list[0]       # read list first value and load dict 
 url = _dict_1['url']     # read 'url' key from dict 

我用过这个,现在为我工作

json_object = json.loads(response.content.decode("utf-8"))['list'][0]['localPercentDynamicObjectsUsed']

我用过这个,现在为我工作

json_object = json.loads(response.content.decode("utf-8"))['list'][0]['localPercentDynamicObjectsUsed']

您知道该对象中唯一的键是“updates”,对吗?您知道该对象中唯一的键是“updates”,对吗?
updates
键在字典列表中有值。您需要传递
updates
的索引,然后访问
url
键。
updates
键在字典列表中有值。您需要传递
更新的索引
,然后访问
url
键。