2013-12-16 36 views
-1

我希望爲具有值爲「Controller」的鍵「標題」的孩子搜索此JSON字典。然後,當發現時,我希望從這個孩子的關鍵「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} 
    } 
+2

定義_find_。找到它的父母,它的關鍵,它的深度? – keyser

+0

是的,對不起。我希望找到它的「tid」鍵的價值。 – fredrik

+0

你有什麼嘗試?另外,你能給出一兩個具體的輸入輸出例子嗎?另外,這是JSON? –

回答

2

遞歸你JSON和尋找控制器。這段代碼對於json的結構應該是不可知的,橫向排列子字典和字典,併產生第一個字典的tid,其中包含具有值「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'。

0

我想你想要一個遞歸函數的東西沿着這條線:

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