2017-04-14 7 views
0

を知らなくても、私はこのJSONを持っている:のpython: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'} 

は私だけsheetsのためにJSONの外にtitle Sを取得するにはどうすればよいですか?私は

入力のような何かをしたい: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

それは私が構造を知っていれば...私は構造を見て、単に 'タイトル'を検索したくないのですが?私は一般的なソリューションを探しています。 – jason

+0

編集内容には解決策がハードコードされています。辞書が大きい場合には、何らかの 'for'ループが必要です。あるいは、入力に忍耐が必要です。 – roganjosh

関連する問題