2017-04-14 50 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