2017-04-14 34 views
0

我有這樣的JSON:蟒蛇:檢索JSON數據的部分,不知道結構

{u'spreadsheetId': u'19CugmHB1Ds6n1jBy4Zo4hk_k4sQsTmOFfccxRc2qo', 
    u'properties': {u'locale': u'en_US', u'timeZone': u'Asia/Hong_Kong', 
    u'autoRecalc': u'ON_CHANGE', u'defaultFormat': {u'padding': {u'top': 2, u'right': 3, u'left': 3, u'bottom': 2}, u'textFormat': {u'foregroundColor': {}, u'bold': False, u'strikethrough': False, u'fontFamily': u'arial,sans,sans-serif', u'fontSize': 10, u'italic': False, u'underline': False}, u'verticalAlignment': u'BOTTOM', u'backgroundColor': {u'blue': 1, u'green': 1, u'red': 1}, u'wrapStrategy': u'OVERFLOW_CELL'}, u'title': u'test pygsheets API V4'}, u'sheets': [{u'properties': {u'sheetType': u'GRID', u'index': 0, u'sheetId': 0, u'gridProperties': {u'columnCount': 26, u'rowCount': 1000}, u'title': u'IO'}}, {u'basicFilter': {u'range': {u'endRowIndex': 978, u'startRowIndex': 2, u'sheetId': 1704577069, u'startColumnIndex': 1, u'endColumnIndex': 9}, u'sortSpecs': [{u'sortOrder': u'ASCENDING', u'dimensionIndex': 1}, {u'sortOrder': u'ASCENDING', u'dimensionIndex': 4}, {u'sortOrder': u'ASCENDING', u'dimensionIndex': 5}, {u'sortOrder': u'ASCENDING', u'dimensionIndex': 8}, {u'sortOrder': u'ASCENDING', u'dimensionIndex': 3}, {u'sortOrder': u'ASCENDING', u'dimensionIndex': 7}, {u'sortOrder': u'ASCENDING', u'dimensionIndex': 2}]}, u'properties': {u'sheetType': u'GRID', u'index': 1, u'title': u'books', u'gridProperties': {u'columnCount': 22, u'rowCount': 978, u'frozenColumnCount': 3, u'hideGridlines': True, u'frozenRowCount': 3}, u'tabColor': {u'blue': 1}, u'sheetId': 1704577069}}], u'spreadsheetUrl': u'https://docs.google.com/spreadsheets/d/1CugmHB1Ds6n1jBy4Zo4hk_k4sQsTmOFfccxRc2qo/edit'} 

如何獲得title出去了的JSON僅售sheets?我想是這樣

輸入:results.get('title')

輸出:['IO','books']

我不知道如何平衡的辦法,因爲嵌套結構。這提醒了一個html節點類型結構。所以我需要某種類型的搜索功能?

有沒有辦法在不查看結構的情況下到達title節點?有點像xpath搜索類型的功能?我之前使用過beautifulsoup,您可能不知道結構並通過搜索取出部分數據。

+0

您到目前爲止嘗試過什麼?你的代碼嘗試遇到什麼問題? – idjaw

+0

你應該包括你已經嘗試過的代碼。這與嵌套字典無異 – roganjosh

+3

實際上,它是一個字典,而不是JSON。 – roganjosh

回答

3

hexerei軟件的解決方案這將給你所需的輸出:

print [x['properties'].get('title') for x in results['sheets']] 

此回報:[u'IO', u'books']

+0

我將不得不知道'屬性'是結構的一部分....你能不知道'屬性'呢? – jason

+1

@jason這是可能的,是的,但通常與JSON,你會想提前知道一個對象的結構。 JSON解析器不帶有xpath等價物。你需要這樣的東西(我以前沒有用過):https://pypi.python.org/pypi/jsonpath-rw – jordanm

1

這應該工作:

a = {your json/dict?} 
print(a['properties']['title']) # prints 'test pygsheets API V4' 
print(a['sheets'][0]['properties']['title']) #prints 'IO' 
print(a['sheets'][1]['properties']['title']) # prints 'books' 

編輯: 未知結構:

def find_in_obj(obj, condition, path=None): 

    if path is None: 
     path = [] 

    # In case this is a list 
    if isinstance(obj, list): 
     for index, value in enumerate(obj): 
      new_path = list(path) 
      for result in find_in_obj(value, condition, path=new_path): 
       yield result 

    # In case this is a dictionary 
    if isinstance(obj, dict): 
     for key, value in obj.items(): 
      new_path = list(path) 
      for result in find_in_obj(value, condition, path=new_path): 
       yield result 

      if condition == key: 
       new_path = list(path) 
       new_path.append(value) 
       yield new_path 

results = [] 
for item in find_in_obj(a, 'title'): 
    results.append(item) 
print(results) #prints [['test pygsheets API V4'], ['IO'], ['books']] 

從修改:在Find all occurrences of a key in nested python dictionaries and lists

+0

那裏有多個'title'鍵。 – roganjosh

+0

這就是如果我知道結構...如果我不想看結構,只想通過'titles'搜索呢?我正在尋找一個通用的解決方案。 – jason

+0

您的編輯已經硬編碼了一個解決方案。如果字典很大,你需要某種「for」循環或者輸入一些耐心。 – roganjosh