2012-03-28 105 views
1

我有一個未知深度和結構的字典。它可能包含更多字典,字典列表等。它是通過反序列化由另一個系統創建的一些JSON輸入創建的。在該字典的不同位置有一個關鍵字,也許有多個具有相同名稱的關鍵字。我想獲得每個鍵的值並理想地更新它們。獲取未知深度的任意字典中的密鑰值

鑑於這樣的目錄結構:

{ 
    "tags":{ 
    "social-1":{ 
     "email":True, 
     "twitter":True, 
     "facebook":True, 
     "linkedin":True, 
     "type":"social" 
    }, 
    "primary":{ 
     "type":"flexible", 
     "width":"auto", 
     "blocks":[ 
     { 
      "type":"combo", 
      "data":{ 
      "styles":{ 
       "margin":"10", 
       "padding":"0", 
       "borderColor":"#000", 
       "borderWidth":"0", 
       "borderStyle":"solid", 
       "backgroundColor":"transparent", 
       "width":"auto" 
      }, 
      "placeholder":True, 
      "headline":{ 
       "visible":False 
      }, 
      "subHeadline":{ 
       "visible":False 
      }, 
      "imageHolder":{ 
       "visible":True, 
       "value":[ 
       { 
        "url":None, 
        "caption":None, 
        "width":220, 
        "height":140, 
        "padding":10, 
        "alt":"", 
        "href":None, 
        "filePath":None, 
        "sizing":"original", 
        "source":"disk", 
        "displayWidth":200, 
        "displayHeight":140, 
        "displayPadding":{ 
        "left":10, 
        "top":0, 
        "right":10, 
        "bottom":10 
        } 
       } 
       ], 
       "smartSizing":True, 
       "captions":False, 
       "captionDefault":None 
      }, 
      "content":{ 
       "visible":True, 
       "value":"<p>Your text here.</p>" 
      }, 
      "imagePosition":"left", 
      "textWrap":False, 
      "type":"combo" 
      } 
     }, 
     { 
      "type":"image", 
      "data":{ 
      "styles":{ 
       "margin":"10", 
       "padding":"0", 
       "borderColor":"#000", 
       "borderWidth":"0", 
       "borderStyle":"solid", 
       "backgroundColor":"transparent", 
       "width":"auto" 
      }, 
      "placeholder":False, 
      "imageHolder":[ 
       { 
       "url":None, 
       "caption":None, 
       "width":0, 
       "height":140, 
       "padding":10, 
       "alt":"", 
       "href":None, 
       "filePath":None, 
       "sizing":"original", 
       "source":"disk", 
       "displayWidth":213, 
       "displayHeight":159, 
       "displayPadding":{ 
        "left":10, 
        "top":10, 
        "right":5, 
        "bottom":10 
       } 
       }, 
       { 
       "url":None, 
       "caption":None, 
       "width":0, 
       "height":140, 
       "padding":10, 
       "alt":"", 
       "href":None, 
       "filePath":None, 
       "displayWidth":213, 
       "displayHeight":159, 
       "source":"disk", 
       "sizing":"original", 
       "displayPadding":{ 
        "left":5, 
        "top":10, 
        "right":5, 
        "bottom":10 
       } 
       }, 
       { 
       "url":None, 
       "caption":None, 
       "width":0, 
       "height":140, 
       "padding":10, 
       "alt":"", 
       "href":None, 
       "filePath":None, 
       "displayWidth":213, 
       "displayHeight":159, 
       "source":"disk", 
       "sizing":"original", 
       "displayPadding":{ 
        "left":5, 
        "top":10, 
        "right":10, 
        "bottom":10 
       } 
       } 
      ], 
      "orientation":"horizontal", 
      "smartSizing":True, 
      "captions":False, 
      "captionDefault":None, 
      "type":"image" 
      } 
     } 
     ] 
    } 
    } 
} 

如何將審查和更新imageHolder鍵的值?

回答

5

您可以使用一個遞歸函數下降到列表和字典值:

def get_all(data, key): 
    sub_iter = [] 
    if isinstance(data, dict): 
     if key in data: 
      yield data[key] 
     sub_iter = data.itervalues() 
    if isinstance(data, list): 
     sub_iter = data 
    for x in sub_iter: 
     for y in get_all(x, key): 
      yield y 
4

遞歸地遍歷樹結構以找到您關心的元素。例如: -

>>> def process(blob): 
...  for (k,v) in blob.items(): 
...   if k == 'imageHolder': 
...    ...do-something... 
...   if isinstance(v, dict): 
...    process(v) 

if isinstance(v,dict):線可能與各種替代品取代,這取決於你期待什麼你的輸入是 - 例如,你可以做鴨打字像if hasattr(v, 'items')

1

您需要遞歸:

def search_key(mykey, mydict): 
     if isinstance(mydict, dict): 
      for key, value in mydict.items(): 
       search_key(mykey, value) 
      if mykey in mydict: 
       print 'found old_value=%r' % mydict[mykey] 
       mydict[mykey]='foo' # update 
     elif isinstance(mydict, (list, tuple)): 
      for value in mydict: 
       search_key(mykey, value) 
    search_key('imageHolder', mydict) 
    mydict={...} # your long json structure 
    search_key('imageHolder', mydict) 
相關問題