在 Python 中解析 JSON 结果时出现 KeyError
Posted
技术标签:
【中文标题】在 Python 中解析 JSON 结果时出现 KeyError【英文标题】:KeyError when parsing JSON results in Python 【发布时间】:2021-08-02 20:36:18 【问题描述】:我在 Python 中解析 JSON
结果时遇到了 KeyError
。我已经搜索了与 KeyError 相关的其他问题,但到目前为止没有一个答案有帮助。当我发出网络请求时
https://property.melissadata.net/v4/WEB/LookupProperty?id=MY_LICENSE_KEY&t=&cols=GrpPrimaryOwner&format=json&ff=5933 NE Garfield Avenue, Portland, OR, 97211
从浏览器返回结果
"Version":"5.0.1.1043","TransmissionResults":"","TotalRecords":1,"Records":["Results":"YS02,YS07,YC01","Parcel":"FIPSCode":"41051","UnformattedAPN":"R243379","FormattedAPN":"R243379","Legal":,"PropertyAddress":,"ParsedPropertyAddress":,"PrimaryOwner":"Name1Full":"PORTLAND PIEDMONT GUESTHOUSE L","Name1First":"","Name1Middle":"","Name1Last":"PORTLAND PIEDMONT GUESTHOUSE L"
...
Python 代码的结果
import requests
import pprint
# api-endpoint
URL = “https://property.melissadata.net/v4/WEB/LookupProperty?id=MY_LICENSE_KEY&t=&cols=GrpPrimaryOwner&format=json”
# location given here
location = "5933 NE Garfield Avenue, Portland, OR 97211"
# defining a params dict for the parameters to be sent to the API
PARAMS = 'address':location
# sending get request and saving the response as response object
r = requests.get(url = URL, params = PARAMS)
# extracting data in json format
data = r.json()
pprint.pprint(data)
name = data['Records'][0]['PrimaryOwner'][0]['Name1Full'][0]
print('name: ' + name)
是
'Records': ['Results': 'YE01'],
'TotalRecords': 1,
'TransmissionResults': '',
'Version': '5.0.1.1043'
>>> name = data['Records'][0]['PrimaryOwner'][0]['Name1Full'][0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'PrimaryOwner'
>>> print('name: ' + name)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "list") to str
我想从 JSON 结果中提取“PORTLAND PIEDMONT GUESTHOUSE L
”的 Name1Full 值。感谢您提供的任何帮助。
解决方案:
当PARAMS = 'address':location
替换为PARAMS = 'ff':location
时,此方法有效
【问题讨论】:
【参考方案1】:鉴于您提供的json
,我相信您应该尝试一下:
full_name = data['Records'][0]['PrimaryOwner']['Name1Full']
让我们看看它是如何工作的:
[Records][0]
访问:
"Results":"YS02,YS07,YC01","Parcel":"FIPSCode":"41051","UnformattedAPN":"R243379","FormattedAPN":"R243379","Legal":,"PropertyAddress":,"ParsedPropertyAddress":,"PrimaryOwner":"Name1Full":"PORTLAND PIEDMONT GUESTHOUSE L","Name1First":"","Name1Middle":"","Name1Last":"PORTLAND PIEDMONT GUESTHOUSE L"...
然后我们需要看到 Name1Full
是 PrimaryOwner
键的值,因此 [`Records'][0]['PrimaryOwner'] 访问:
"Name1Full":"PORTLAND PIEDMONT GUESTHOUSE L","Name1First":"","Name1Middle":"","Name1Last":"PORTLAND PIEDMONT GUESTHOUSE L"...
最后我们添加键 Name1Full
来访问具体值 [`Records'][0]['PrimaryOwner']['Name1First']:
返回:
"PORTLAND PIEDMONT GUESTHOUSE L"
【讨论】:
【参考方案2】:您在问题的第二部分中漂亮打印的 JSON 没有 'PrimaryOwner'
字段,这就是您收到关键错误的原因。
'Records': ['Results': 'YE01'],
'TotalRecords': 1,
'TransmissionResults': '',
'Version': '5.0.1.1043'
我认为问题是:为什么您的程序获得的 JSON 数据与您的网络浏览器不同?我无法进一步帮助您,但也许其他人可以。
【讨论】:
感谢大家的帮助。这在PARAMS = 'address':location
被替换为 PARAMS = 'ff':location
时有效【参考方案3】:
我简化了问题。你没有得到你认为你得到的东西。下面这个sn-p重现了这个问题,我把多余的代码去掉了。
data = 'Records': ['Results': 'YE01'],
'TotalRecords': 1,
'TransmissionResults': '',
'Version': '5.0.1.1043'
name = data['Records'][0]['PrimaryOwner'][0]['Name1Full'][0]
print('name: ' + name)
【讨论】:
【参考方案4】:当 PARAMS = 'address':location 替换为 PARAMS = 'ff':location
【讨论】:
以上是关于在 Python 中解析 JSON 结果时出现 KeyError的主要内容,如果未能解决你的问题,请参考以下文章
Web Scraping:在 Python 中解析 JSON 时出现 KeyError
JSON解析时出现:TypeError: the JSON object must be str, bytes or bytearray, not NoneType
解析“......没有方法'replace'”时出现JSON错误
在 create-react-app 创建的环境中获取本地 JSON 文件时出现 JSON 解析错误