繁体   English   中英

Python 检查嵌套的 JSON 键值是否存在

[英]Python to check existence of nested JSON key-value

我有给定格式的 JSON ,我的目标是确定键值对是否

“name”:“Important1”,“name”:“Important2”,“name”:“Important3”是否存在于“TAG3”下?

我需要通过 Python 执行此操作。 有什么建议么? 我尝试了各种建议的示例,但没有一个能够解析给定的示例。

{
  "BASE": {
    "TAG1": [
      {
        "attr": {
          "name": "val1",
          "place": "val1"
        },
        "r1": 65535,
        "r2": 444
      },
      {
        "attr": {
          "name": "Important1",
          "place": "RPlace"
        },
        "r1": 65535,
        "r2": 109
      },
      {
        "attr": {
          "name": "Important2",
          "place": "GPlace"
        },
        "r1": 65535,
        "r2": 453
      },
      {
        "attr": {
          "name": "Important3",
          "place": "BPlace"
        },
        "r1": 65535,
        "r2": 109
      }
    ],
    "my-id": 2412922,
    "TAG2": [
      {
        "attr": {
          "name": "val1",
          "place": "val1"
        },
        "r1": 65535,
        "r2": 444
      },
      {
        "attr": {
          "name": "Important1",
          "place": "RPlace"
        },
        "r1": 65535,
        "r2": 109
      },
      {
        "attr": {
          "name": "Important2",
          "place": "GPlace"
        },
        "r1": 65535,
        "r2": 453
      },
      {
        "attr": {
          "name": "Important3",
          "place": "BPlace"
        },
        "r1": 65535,
        "r2": 109
      }
    ],
    "TAG3": [
      {
        "name": "POSITION",
        "place": "POSITION",
        "type": "M96"
      },
      {
        "name": "val1",
        "place": "val1"
      },
      {
        "name": "Important1",
        "place": "RPlace"
      },
      {
        "name": "Important2",
        "place": "GPlace"
      },
      {
        "name": "Important3",
        "place": "BPlace"
      }
    ],
    "out-box": {
      "r1": [
        6.2,
        7.3
      ],
      "r2": [
        6.2,
        7.3
      ]
    },
    "t1": "hldsadh1",
    "t2": 0,
    "t3": 20,
    "string-attr": [
      1,
      16
    ],
    "r1-chk": 342,
    "my-size": 34,
    "where": [
      2.9,
      54.9
    ],
    "sometag": 2,
    "revision": 0,
    "nodata": [
      0,
      0
    ],
    "noscale": [
      0.001,
      0.001
    ],
    "time-val": 3444
  }
}

我尝试使用下面的 function 但由于我有限的 python 知识我有点卡住了

def keys_exists(element, *keys):
    """
    Check if *keys (nested) exists in `element` (dict).
    """
    if type(element) is not dict:
        raise AttributeError('keys_exists() expects dict as first argument.')
    if len(keys) == 0:
        raise AttributeError('keys_exists() expects at least two arguments, one given.')

    _element = element
    for key in keys:
        try:
            _element = _element[key]
        except KeyError:
            return False
    return True

如果我将 JSON 存储在名为data1的变量中,那么我试图在 function 之上使用

keys_exists(data1, "BASE", "TAG3", "name")))

请随时丢弃我的版本并分享您自己的版本。

您似乎在迭代所有内容,而不是试图提取您需要的内容。 我会创建一个 function 来获取标签的所有名称,然后您可以检查特定名称是否存在。

data = json.loads("""
<the json string goes here>
""")

def get_names_by_tag(tag):
    return [item["name"] for item in data["BASE"][tag]]

tag3_names = get_names_by_tag("TAG3")
# tag3_names = ['POSITION', 'val1', 'Important1', 'Important2', 'Important3']

important_names = ["Important1", "Important2", "Important3"]

if all(important_name in tag3_names for important_name in important_names):
    # do something

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM