繁体   English   中英

在我们不知道有多少嵌套对象具有相同键的情况下,如何遍历 json object 的特定键?

[英]How can I loop over a specific key of a json object where we don't know how many nested objects have the same key?

我有以下 JSON 结构:

{
    "name": "Birds",
    "title": "Birds",
    "default_language": "default",
    "id_string": "Birds",
    "type": "survey",
    "children": [
        {
            "type": "text",
            "name": "your_name",
            "label": "1. What is your name?"
        }
    ]
}

我想要做的是添加一个具有以下结构的新表:

label                   name           

1. What is your name?   your_name

但有时,由于我的数据形状是动态的,并且会删除一些输入或添加新的输入,JSON 文件将如下所示:

{
  "name": "Birds",
  "title": "Birds",
  "default_language": "default",
  "id_string": "Birds",
  "type": "survey",
  "children": [
    {
      "type": "text",
      "name": "your_name",
      "label": "1. What is your name?",
      "children": [
        {
          "type": "text",
          "name": "your_favorite_food",
          "label": "2. What is your favorite food?",
          "children": [
            {
              "type": "text",
              "name": "your_favorite_drink",
              "label": "3. What is your favourite drink with it?"
            }
          ]
        }
      ]
    },
    {
      "children": [
        {
          "type": "text",
          "name": "your_country",
          "label": "Where are you coming from?"
        }
      ]
    }
  ]
}

在这种情况下,表格看起来像这样:

在此处输入图像描述

如果我不知道该键存在多少嵌套对象,如何使用 Python 遍历特定键?

你可以recursively找到它

d = {
  "name": "Birds",
  "title": "Birds",
  "default_language": "default",
  "id_string": "Birds",
  "type": "survey",
  "children": [
    {
      "type": "text",
      "name": "your_name",
      "label": "1. What is your name?",
      "children": [
        {
          "type": "text",
          "name": "your_favorite_food",
          "label": "2. What is your favorite food?",
          "children": [
            {
              "type": "text",
              "name": "your_favorite_drink",
              "label": "3. What is your favourite drink with it?"
            }
          ]
        }
      ]
    },
    {
      "children": [
        {
          "type": "text",
          "name": "your_country",
          "label": "Where are you coming from?"
        }
      ]
    }
  ]
}


def get_data(data, l, depth=0, prefix = ''):
    if(isinstance(data, dict)):
        depth += 1
        label = data.get('label')
        name = data.get('name')
        if(label and name):
            if(depth == 1):
                l.append((label, name))

            prefix = name if prefix=='' else prefix + '/' + name
                
        if('children' in data.keys()):

            get_data(data['children'], l, depth, prefix)
        else:

            l.append((label, prefix))
            
    elif(isinstance(data, list)):

        for each_data in data:
            get_data(each_data, l, depth,  prefix)

l = []
get_data(d['children'], l) #d is your actual data

output

[('1. What is your name?', 'your_name'),
 ('3. What is your favourite drink with it?',
  'your_name/your_favorite_food/your_favorite_drink'),
 ('Where are you coming from?', 'your_country')]

暂无
暂无

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

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