2015-10-01 61 views
-1

there 我使用python從Oanda API中獲取數據,並以如下格式獲取數據:,我不知道如何解壓縮它,所以我可以得到一個時間序列,是有人可以幫助我嗎?謝謝!Python如何解壓http響應數據

{ 
    "instrument" : "EUR_USD", 
    "granularity" : "M15", 
    "candles" : [ 
     { 
      "time" : "2015-10-01T19:15:00.000000Z", 
      "openBid" : 1.11833, 
      "openAsk" : 1.11846, 
      "highBid" : 1.1186, 
      "highAsk" : 1.11874, 
      "lowBid" : 1.11825, 
      "lowAsk" : 1.1184, 
      "closeBid" : 1.1184, 
      "closeAsk" : 1.11857, 
      "volume" : 205, 
      "complete" : true 
     }, 
     { 
      "time" : "2015-10-01T19:30:00.000000Z", 
      "openBid" : 1.11844, 
      "openAsk" : 1.1186, 
      "highBid" : 1.11924, 
      "highAsk" : 1.1194, 
      "lowBid" : 1.11839, 
      "lowAsk" : 1.11857, 
      "closeBid" : 1.11908, 
      "closeAsk" : 1.11926, 
      "volume" : 290, 
      "complete" : true 
     }, 
     { 
      "time" : "2015-10-01T19:45:00.000000Z", 
      "openBid" : 1.11906, 
      "openAsk" : 1.11922, 
      "highBid" : 1.11933, 
      "highAsk" : 1.1195, 
      "lowBid" : 1.11868, 
      "lowAsk" : 1.11883, 
      "closeBid" : 1.11868, 
      "closeAsk" : 1.11883, 
      "volume" : 206, 
      "complete" : true 
     }, 
     { 
      "time" : "2015-10-01T20:00:00.000000Z", 
      "openBid" : 1.11865, 
      "openAsk" : 1.11879, 
      "highBid" : 1.11884, 
      "highAsk" : 1.119, 
      "lowBid" : 1.11827, 
      "lowAsk" : 1.11844, 
      "closeBid" : 1.11833, 
      "closeAsk" : 1.11848, 
      "volume" : 114, 
      "complete" : true 
     }] 
} 
+0

這僅僅是JSON。 Python有一個內置的json庫。 –

+0

我是新來的蟒蛇,感謝您的提示 – louis

回答

0

您可以使用標準的JSON模塊(見here關於如何進行編碼和解碼一個很好的解釋)

import json 
s = ''' 
    your json sample 
    ''' 
d = json.loads(s) 

,那麼你可以在d字典中訪問結構的元素

eg

d['instrument'] 

d['candles'][0]['lowBid'] 
+0

你是正確的,謝謝你拯救我的屁股 – louis

+0

我不得不說你是一個天才,確切地知道我的問題是什麼。問題解決了,謝謝 – louis

+0

請大師擦接受/ upvote按鈕,然後:D – Pynchia

0

您正在尋找json模塊

from json import loads 

mydata = """{ 
    "instrument": "EUR_USD", 
    "granularity": "M15", 
    "candles": [ 
     { 
      "time": "2015-10-01T19:15:00.000000Z", 
      "openBid": 1.11833, 
      "openAsk": 1.11846, 
      "highBid": 1.1186, 
      "highAsk": 1.11874, 
      "lowBid": 1.11825, 
      "lowAsk": 1.1184, 
      "closeBid": 1.1184, 
      "closeAsk": 1.11857, 
      "volume": 205, 
      "complete": true 
     } 
    ] 
}""" 

jdata = json.loads(mydata) 
jdata['instrument'] 
>>> u'EUR_USD 
+0

你說得對,謝謝你救我的屁股 – louis