Python如何解压http响应数据

时间:2015-10-01 20:19:37

标签: python

有 我使用python从Oanda API中提取数据,并以下面的格式获取数据:,我不知道如何解压缩它,所以我可以得到一个时间序列,是否有人可以帮助我?谢谢!

{
    "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
        }]
}

2 个答案:

答案 0 :(得分:0)

您可以使用标准的json模块(有关如何编码和解码的详细说明,请参阅here

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

然后您可以在d字典

中访问结构的元素

e.g。

d['instrument']

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

答案 1 :(得分: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