如何在字典中查找深度未知的孩子?

2024-04-18 22:36:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望在这个JSON字典中搜索键“title”和值“Controller”的子元素。然后,当找到时,我希望从这个孩子的键“tid”中获取值。在下面的示例字典中,值将为“7”。在

子“控制器”可能在树的不同深度结束。在

这是JSON。我一直致力于寻找一种方法来遍历未知深度的儿童。在

  {
  "users": {
   "fredrik": {
    "J1312160092": {
     "data": {
         "somedata": 0,
         "someotherdata": [36, 1, 2, 0]
      },

     "children": [
        { "#": "T1",
           "children": [
              { "#": "T2",
                 "children": [
                    { "#": "T3",
                       "children": [
                          { "#": "T4",
                             "children": [],
                             "data": {
                                "tid": 4,
                                "title": "Pre-job: Sub-task 1",
                                "state": "+D",
                                "cids": [1]
                             }
                          },
                          { "#": "T5",
                             "children": [],
                             "data": {
                                "tid": 5,
                                "title": "Pre-job: Sub-task 2",
                                "state": "+D",
                                "cids": [2]
                             }
                          }
                       ],
                       "data": {
                          "serialsubtasks": 0,
                          "tid": 3,
                          "title": "Pre-job",
                          "state": "+A",
                          "cids": [3]
                       }
                    },
                    { "#": "T6",
                       "children": [
                          { "#": "T7",
                             "children": [
                                { "#": "T8",
                                   "children": [],
                                   "data": {
                                      "tid": 8,
                                      "title": "Master: Frame 1-1, camera(s): camera1, camera2",
                                      "state": "-B",
                                      "cids": [4]
                                   }
                                }
                             ],
                             "data": {
                                "tid": 7,
                                "title": "Controller",
                                "state": "-B",
                                "cids": [5]
                             }
                          },
                          { "#": "T9",
                             "children": [
                                { "#": "T10",
                                   "children": [],
                                   "data": {
                                      "tid": 10,
                                      "title": "Frame 1",
                                      "state": "-B",
                                      "cids": [6]
                                   }
                                },
                                { "#": "T11",
                                   "children": [],
                                   "data": {
                                      "tid": 11,
                                      "title": "Frame 2",
                                      "state": "-B",
                                      "cids": [7]
                                   }
                                },
                                { "#": "T12",
                                   "children": [],
                                   "data": {
                                      "tid": 12,
                                      "title": "Frame 3",
                                      "state": "-B",
                                      "cids": [8]
                                   }
                                }
                             ],
                             "data": {
                                "tid": 9,
                                "title": "Frame holder",
                                "state": "-B"
                             }
                          }
                       ],
                       "data": {
                          "serialsubtasks": 0,
                          "tid": 6,
                          "title": "Frame job",
                          "state": "-B"
                       }
                    }
                 ],
                 "data": {
                    "serialsubtasks": 1,
                    "tid": 2,
                    "title": "Blocker",
                    "state": "-B"
                 }
              }
           ],
           "data": {
              "tid": 1,
              "title": "Main job",
              "state": "-B"
           }
        }
     ]
  }

   }
  },
  "recordlimit": {"limit": 1, "truncated": 0}
  }

Tags: jsontaskdata字典titlejobpreframe
2条回答

递归json并查找控制器。这个片段应该与json的结构无关,交叉子列表和字典,并生成第一个字典的tid,其中包含一个键“title”,值为“Controller”。在

def findTid (j):
    if isinstance (j, dict):
        if 'title' in j and j ['title'] == 'Controller':
            return j ['tid']
        for v in j.values ():
            r = findTid (v)
            if r: return r
    if isinstance (j, list):
        for e in j:
            r = findTid (e)
            if r: return r

用你的数据调用它返回'7'。在

我想你需要一个递归函数,大致如下:

def find(tree):
    if tree["data"]["title"] == "Controller":
         return tree["data"]["tid"]
    if "children" in tree:
        return 
    for child in tree["children"]:
        f = find(child)
        if f:
             return f

相关问题 更多 >