json_normalize: AttributeError 列表对象没有属性 'value'
json_normalize: AttributeError list obj has no attribute 'value'
我正在处理来自 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'.
我正在处理来自 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'.