【问题标题】:parsing api complex json data recursively using python使用python递归解析api复杂的json数据
【发布时间】:2019-08-28 04:03:47
【问题描述】:

以key为输入并在整个json数据中搜索该key并返回该key的键值对列表的方法

此方法适用于普通键值对 ('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'
]

【问题讨论】:

  • 附加输入数据
  • 您的代码示例中的格式令人怀疑。看起来 results = extract(obj, arr, key) 和随后的 return 应该是 extract 函数的一部分,但它们不是。

标签: python json recursion data-structures request


【解决方案1】:

您只是打开文件,而不是真正从中读取。因此,要将其作为 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)

【讨论】:

    猜你喜欢
    • 2019-08-28
    • 1970-01-01
    • 2021-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    相关资源
    最近更新 更多