如何从Python中的API从JSON提取此数据?

时间:2018-10-11 05:10:07

标签: python json python-3.x api

我正在使用this API,它返回一个JSON对象,如:

[{"date":1538352000,"high":543.888,"low":520.89920292,"open":532.41173358,"close":532.71432488,"volume":792203.12027095,"quoteVolume":1486.26362316,"weightedAverage":533.01655771},{"date":1538438400,"high":553,"low":523.82274418,"open":535.06483,"close":533.15796645,"volume":1450475.8150522,"quoteVolume":2682.34573456,"weightedAverage":540.74901544},{"date":1538524800,"high":533.60658309,"low":506.22210308,"open":531.24219955,"close":517.00043658,"volume":945028.64730972,"quoteVolume":1827.1757435,"weightedAverage":517.20730787},{"date":1538611200,"high":533.00000001,"low":509.587,"open":516.82852718,"close":513.21852291,"volume":544265.53254745,"quoteVolume":1041.87603086,"weightedAverage":522.38991629},{"date":1538697600,"high":530,"low":512.7298752,"open":514.75817848,"close":521.2431,"volume":645308.06820112,"quoteVolume":1243.52076205,"weightedAverage":518.93630399},{"date":1538784000,"high":523.39414,"low":503.53380007,"open":523.39414,"close":511.77607278,"volume":473719.42567386,"quoteVolume":923.75069854,"weightedAverage":512.82172389},{"date":1538870400,"high":526.20546417,"low":505.26000001,"open":510.54452549,"close":522.92723267,"volume":402055.92168411,"quoteVolume":779.44882881,"weightedAverage":515.82080416},{"date":1538956800,"high":536.58563365,"low":518.82687001,"open":522.04549871,"close":530.855282,"volume":696173.37857725,"quoteVolume":1313.23574261,"weightedAverage":530.12064474},{"date":1539043200,"high":531.4304796,"low":516.90000001,"open":530.5,"close":521.07257237,"volume":296723.0286641,"quoteVolume":568.04170907,"weightedAverage":522.36134059},{"date":1539129600,"high":521.3392031,"low":510.00000001,"open":520.23843786,"close":516.04400658,"volume":372294.49209797,"quoteVolume":721.23650102,"weightedAverage":516.18919948},{"date":1539216000,"high":515.63796825,"low":452.00000015,"open":515.63796825,"close":463.81882794,"volume":712051.78093963,"quoteVolume":1499.40313563,"weightedAverage":474.89015063}]

并且在Python中,我试图获得第二高的“高”。在这种情况下,如果您在单击该链接时查看数据,则结果为"521.3392031"

我正在努力做到这一点,以便它总是能抢到第二高的位置。

我将使用类似这样的东西:(这有助于我从另一个poloniex API中获取其他数据)

poloniexPrices = urlopen('https://poloniex.com/public?command=returnChartData&currencyPair=USDT_BCH&start=1538352000&period=86400').read()

poloniexjson = json.loads(poloniexPrices)

poloniexlastP = poloniexjson['BLANK']['BLANK']

print (poloniexlastP)

但我只是不知道在这种情况下该怎么办。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

如果列表中的字典始终按照看起来的时间顺序排列,则只需执行以下操作即可:

print(poloniexjson[-2]['high'])

否则,您可以先按date键对字典进行排序:

print(sorted(poloniexjson, key=lambda d: d['date'])[-2]['high'])