以键作为输入并在整个json数据中搜索该键并返回该键的键值对列表的方法
此方法适用于普通键值对('key':'some value'),但是如果键的值是列表或字典('keya':'[1,2,3]')它返回空列表
filedata=open('testdata.json','r')
filedata=json.loads(filedata)
def extract_values(obj, key):
"""Pull all values of specified key from nested JSON."""
arr = []
def extract(obj, arr, key):
"""Recursively search for values of key in JSON tree."""
if isinstance(obj, dict):
for k, v in obj.items():
if isinstance(v, (dict, list)):
extract(v, arr, key)
elif k == key:
arr.append(v)
elif isinstance(obj, list):
for item in obj:
extract(item, arr, key)
return arr
results = extract(obj, arr, key)
return results
z=extract_values(filedata,'text')
print(z)
输入数据:
{
"destination_addresses": [
"Washington, DC, USA",
"Philadelphia, PA, USA",
"Santa Barbara, CA, USA",
"Miami, FL, USA",
"Austin, TX, USA",
"Napa County, CA, USA"
],
"origin_addresses": [
"New York, NY, USA"
],
"rows": [
{
"elements": [
{
"distance": {
"text": "227 mi",
"value": 365468
},
"duration": {
"text": "3 hours 54 mins",
"value": 14064
},
"status": "OK"
},
{
"distance": {
"text": "94.6 mi",
"value": 152193
},
"duration": {
"text": "1 hour 44 mins",
"value": 6227
},
"status": "OK"
},
{
"distance": {
"text": "2,878 mi",
"value": 4632197
},
"duration": {
"text": "1 day 18 hours",
"value": 151772
},
"status": "OK"
},
{
"distance": {
"text": "1,286 mi",
"value": 2069031
},
"duration": {
"text": "18 hours 43 mins",
"value": 67405
},
"status": "OK"
},
{
"distance": {
"text": "1,742 mi",
"value": 2802972
},
"duration": {
"text": "1 day 2 hours",
"value": 93070
},
"status": "OK"
},
{
"distance": {
"text": "2,871 mi",
"value": 4620514
},
"duration": {
"text": "1 day 18 hours",
"value": 152913
},
"status": "OK"
}
]
}
],
"status": "OK"
}
它返回一个空列表:
[]
预期输出:
[
'227 mi',
'3 hours 54 mins',
'94.6 mi',
'1 hour 44 mins',
'2,878 mi',
'1 day 18 hours',
'1,286 mi',
'18 hours 43 mins',
'1,742 mi',
'1 day 2 hours',
'2,871 mi',
'1 day 18 hours'
]
答案 0 :(得分:0)
您只是打开文件,而不是真正从中读取文件。因此,要将其作为json读取并读取,可以使用json.load()
。
这是修改后的代码:
import json
filedata = open('testdata.json','r')
filedata = json.load(filedata)
def extract_values(obj, key):
"""Pull all values of specified key from nested JSON."""
arr = []
def extract(obj, arr, key):
"""Recursively search for values of key in JSON tree."""
if isinstance(obj, dict):
for k, v in obj.items():
if isinstance(v, (dict, list)):
extract(v, arr, key)
elif k == key:
arr.append(v)
elif isinstance(obj, list):
for item in obj:
extract(item, arr, key)
return arr
results = extract(obj, arr, key)
return results
z = extract_values(filedata,'text')
print(z)