2017-02-26 9 views
0

特定のデータを抽出するJsonファイルがあります。以下はJSONファイルです:jsonファイルの解析 - Python

{ 
    "results": [ { 
     "alternatives": [ { 
      "word_confidence": [ 
       [ "Ryan", 0.335 ], 
       [ "how's", 0.589 ], 
       [ "the", 1.0 ], 
       [ "weather", 1.0 ], 
       [ "in", 1.0 ], 
       [ "New", 1.0], 
       [ "York", 0.989 ], 
       [ "today", 0.987 ] 
      ], 
      "confidence": 0.795, 
      "transcript": "Ryan how's the weather in New York today " 
     } ], 
     "final": true 
    } ], 
    "result_index": 0 
} 

このファイルを解析して「トランスクリプト」を抽出するにはどうすればよいですか?

+0

'輸入JSON。 data = json.loads(json_string) ' –

+0

@stephen 私は を使用しました。import json; 'u'results':[{u'transcript ':u "Ryan、今日のニューヨークの天気はいかがですか"、u'confidence' :0.795、u'word_confidence ':[u'Ryan'、0.335]、 'u'the'、 'u'the'、1.0 '、' u'weather '、1.0'、 'u'in' u'york '、0.989]、[u'today'、0.987]}]、u'final ':True}]、u'result_index':0}、[1.0]、[u'New ' ' 今、キー値「転写」のデータを抽出する必要があります。これを行う方法 –

+0

この解決方法のための解決策 data = json.loads(json_data) text_data = data ["results"] [0] ["代替案]] [0] ["トランスクリプト"] –

答えて

2

json文字列をdictに変換するには、json.loads()を使用します。

コード:

import json 
data = json.loads(json_string) 
transcript = data['results'][0]['alternatives'][0]['transcript'] 

試験データ:その後、転写産物を得るために、同じように、dictに参照

json_string = """ 
{ 
    "results": [ { 
     "alternatives": [ { 
      "word_confidence": [ 
       [ "Ryan", 0.335 ], 
       [ "how's", 0.589 ], 
       [ "the", 1.0 ], 
       [ "weather", 1.0 ], 
       [ "in", 1.0 ], 
       [ "New", 1.0], 
       [ "York", 0.989 ], 
       [ "today", 0.987 ] 
      ], 
      "confidence": 0.795, 
      "transcript": "Ryan how's the weather in New York today " 
     } ], 
     "final": true 
    } ], 
    "result_index": 0 
} 
"""