我正在处理来自 API 响应的 JSON 数据。我正在使用 python 发送带有地址数据的 GET 请求。我想捕获 JSON 响应并转换为数据帧。
目前,我在字典列表中捕获响应。我可以在 r.json 或sample_list[0] 上成功使用 json_normalize,但无法规范化整个列表。我试图避免在循环中创建和附加 df 以提高性能。
sample_list = []
for index, row in sample_df.iterrows():
sample_address = json.dumps(
{
"records": [
{
"attributes": {
"OBJECTID": row['OBJECTID'],
"Address": row['Address'],
"City": row['City'],
"Region": row['Region'],
"Postal": row['Postal']
}
}
]
}
)
r = re.get(url, params = { 'addresses': sample_address, 'f':'pjson'},verify = False)
sample_list.append(r.json()['locations'])
###The Output of r.json for one address
{'locations': [{'address': '2600 Benjamin Franklin Pkwy, Philadelphia, Pennsylvania, 19130',..., 'score': 100}], 'spatialReference': {'latestWkid': 4326, 'wkid': 4326}}
###The sample_list of multiple r.json output
[[{'address': '520 Chestnut St, Philadelphia, Pennsylvania, 19106',
'attributes': {'AddNum': '520',
...},
'location': {'x': -75.14971142634045, 'y': 39.94905972672609},
'score': 100}],
[{'address': '2600 Benjamin Franklin Pkwy, Philadelphia, Pennsylvania, 19130',
'attributes': {'AddNum': '2600',
...,
'location': {'x': -75.17923104567541, 'y': 39.96474536190999},
'score': 100}]]
最佳答案
在sample_list.append(r.json()['locations'])中只需尝试'json_normalize'而不是'r.json'。
关于python - json_标准化: AttributeError list obj has no attribute 'value' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59602312/