簡體   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