Python -通过Kraken API从json中提取/访问信息

kpbwa7wx  于 2022-10-30  发布在  Python
关注(0)|答案(1)|浏览(126)

我正在尝试理解如何使用从de Kraken API获得的json返回。
这是我的代码:

resp = kraken_request("/0/private/TradeBalance", {
    "nonce": str(int(10000*time.time())),
    "asset": "USD"
}, api_key, api_sec)

print("TradeBalance: " + str(resp.json()))

resp = kraken_request("/0/private/OpenOrders", {
    "nonce": str(int(10000*time.time())),
    "asset": "USD"
}, api_key, api_sec)
print()
print("OpenOrders: " + str(resp.json())

这是我的json返回(编辑过,所以它不是真实的数据):

{"error":[],"result":{"open":{"ABCDE-AWSCG-ABCDE":{"refid":null,"userref":0,"status":"open","opentm":1666683580.8848298,"starttm":0,"expiretm":0,"descr":{"pair":"LINKEUR","type":"sell","ordertype":"limit","price":"8.70000","price2":"0","leverage":"none","order":"sell 1.00000000 LINKEUR @ limit 8.70000","close":""},"vol":"1.00000000","vol_exec":"0.00000000","cost":"0.000000","fee":"0.000000","price":"0.000000","stopprice":"0.000000","limitprice":"0.000000","misc":"","oflags":"fcib"},"ABCDE-AHKEK-ABCDE":{"refid":null,"userref":0,"status":"open","opentm":1666683548.8137474,"starttm":0,"expiretm":0,"descr":{"pair":"LINKEUR","type":"sell","ordertype":"limit","price":"8.60000","price2":"0","leverage":"none","order":"sell 1.00000000 LINKEUR @ limit 8.60000","close":""},"vol":"1.00000000","vol_exec":"0.00000000","cost":"0.000000","fee":"0.000000","price":"0.000000","stopprice":"0.000000","limitprice":"0.000000","misc":"","oflags":"fcib"},"ABCDE-POQX7-ABCDE":{"refid":null,"userref":0,"status":"open","opentm":1666683517.207053,"starttm":0,"expiretm":0,"descr":{"pair":"LINKEUR","type":"sell","ordertype":"limit","price":"8.50000","price2":"0","leverage":"none","order":"sell 1.00000000 LINKEUR @ limit 8.50000","close":""},"vol":"1.00000000","vol_exec":"0.00000000","cost":"0.000000","fee":"0.000000","price":"0.000000","stopprice":"0.000000","limitprice":"0.000000","misc":"","oflags":"fcib"},"ABCDE-UADOV-ABCDE":{"refid":null,"userref":0,"status":"open","opentm":1666683490.4299977,"starttm":0,"expiretm":0,"descr":{"pair":"LINKEUR","type":"sell","ordertype":"limit","price":"8.40000","price2":"0","leverage":"none","order":"sell 1.00000000 LINKEUR @ limit 8.40000","close":""},"vol":"1.00000000","vol_exec":"0.00000000","cost":"0.000000","fee":"0.000000","price":"0.000000","stopprice":"0.000000","limitprice":"0.000000","misc":"","oflags":"fcib"},"ABCDE-ABCDE-2JGH3M":{"refid":null,"userref":0,"status":"open","opentm":1666646615.668582,"starttm":0,"expiretm":0,"descr":{"pair":"LINKEUR","type":"sell","ordertype":"limit","price":"8.30000","price2":"0","leverage":"none","order":"sell 1.00000000 LINKEUR @ limit 8.30000","close":""},"vol":"1.00000000","vol_exec":"0.00000000","cost":"0.000000","fee":"0.000000","price":"0.000000","stopprice":"0.000000","limitprice":"0.000000","misc":"","oflags":"fciq"},"ABCDE-623OO-ABCDE":{"refid":null,"userref":0,"status":"open","opentm":1666646572.9639573,"starttm":0,"expiretm":0,"descr":{"pair":"LINKEUR","type":"sell","ordertype":"limit","price":"8.20000","price2":"0","leverage":"none","order":"sell 1.00000000 LINKEUR @ limit 8.20000","close":""},"vol":"1.00000000","vol_exec":"0.00000000","cost":"0.000000","fee":"0.000000","price":"0.000000","stopprice":"0.000000","limitprice":"0.000000","misc":"","oflags":"fciq"},"ABCDE-4EHBG-ABCDE":{"refid":null,"userref":0,"status":"open","opentm":1666646537.55891,"starttm":0,"expiretm":0,"descr":{"pair":"LINKEUR","type":"sell","ordertype":"limit","price":"8.10000","price2":"0","leverage":"none","order":"sell 1.00000000 LINKEUR @ limit 8.10000","close":""},"vol":"1.00000000","vol_exec":"0.00000000","cost":"0.000000","fee":"0.000000","price":"0.000000","stopprice":"0.000000","limitprice":"0.000000","misc":"","oflags":"fciq"},"ABCDE-SPHMF-ABCDE":{"refid":null,"link_id":"ABCDE-YWJVW-ABCDE","userref":0,"status":"open","opentm":1666646453.770994,"starttm":0,"expiretm":0,"descr":{"pair":"LINKEUR","type":"sell","ordertype":"limit","price":"7.99900","price2":"0","leverage":"none","order":"sell 1.00000000 LINKEUR @ limit 7.99900","close":""},"vol":"1.00000000","vol_exec":"0.00000000","cost":"0.000000","fee":"0.000000","price":"0.000000","stopprice":"0.000000","limitprice":"0.000000","misc":"","oflags":"fcib"},"ABCDE-RVWQO-ABCDE":{"refid":null,"userref":0,"status":"open","opentm":1663311467.0131989,"starttm":0,"expiretm":0,"descr":{"pair":"ETHWUSD","type":"sell","ordertype":"limit","price":"1000.000","price2":"0","leverage":"none","order":"sell 357.00000000 ETHWUSD @ limit 1000.000","close":""},"vol":"357.00000000","vol_exec":"0.00000000","cost":"0.0000000","fee":"0.0000000","price":"0.0000000","stopprice":"0.0000000","limitprice":"0.0000000","misc":"","oflags":"fcib"}}}}

现在我还不知道如何使用这个json,我想仔细检查一下,并处理每个项目,如“ABCDE-AWSCG-ABCDE”和“ABCDE-POQX 7-ABCDE”等,对于每个项目的所有底层信息,我不知道如何操作。请注意,我事先不知道项目名称,如“ABCDE-AWSCG-ABCDE”。

rjjhvcjd

rjjhvcjd1#

你应该使用pandas。你可以在一个表中看到所有的数据。这比用json工作要容易得多。

import pandas as pd
import numpy as np
null=np.nan

returned_json={"error":[],"result":{"open":{"ABCDE-AWSCG-ABCDE":{"refid":null,"userref":0,"status":"open","opentm":1666683580.8848298,"starttm":0,"expiretm":0,"descr":{"pair":"LINKEUR","type":"sell","ordertype":"limit","price":"8.70000","price2":"0","leverage":"none","order":"sell 1.00000000 LINKEUR @ limit 8.70000","close":""},"vol":"1.00000000","vol_exec":"0.00000000","cost":"0.000000","fee":"0.000000","price":"0.000000","stopprice":"0.000000","limitprice":"0.000000","misc":"","oflags":"fcib"},"ABCDE-AHKEK-ABCDE":{"refid":null,"userref":0,"status":"open","opentm":1666683548.8137474,"starttm":0,"expiretm":0,"descr":{"pair":"LINKEUR","type":"sell","ordertype":"limit","price":"8.60000","price2":"0","leverage":"none","order":"sell 1.00000000 LINKEUR @ limit 8.60000","close":""},"vol":"1.00000000","vol_exec":"0.00000000","cost":"0.000000","fee":"0.000000","price":"0.000000","stopprice":"0.000000","limitprice":"0.000000","misc":"","oflags":"fcib"},"ABCDE-POQX7-ABCDE":{"refid":null,"userref":0,"status":"open","opentm":1666683517.207053,"starttm":0,"expiretm":0,"descr":{"pair":"LINKEUR","type":"sell","ordertype":"limit","price":"8.50000","price2":"0","leverage":"none","order":"sell 1.00000000 LINKEUR @ limit 8.50000","close":""},"vol":"1.00000000","vol_exec":"0.00000000","cost":"0.000000","fee":"0.000000","price":"0.000000","stopprice":"0.000000","limitprice":"0.000000","misc":"","oflags":"fcib"},"ABCDE-UADOV-ABCDE":{"refid":null,"userref":0,"status":"open","opentm":1666683490.4299977,"starttm":0,"expiretm":0,"descr":{"pair":"LINKEUR","type":"sell","ordertype":"limit","price":"8.40000","price2":"0","leverage":"none","order":"sell 1.00000000 LINKEUR @ limit 8.40000","close":""},"vol":"1.00000000","vol_exec":"0.00000000","cost":"0.000000","fee":"0.000000","price":"0.000000","stopprice":"0.000000","limitprice":"0.000000","misc":"","oflags":"fcib"},"ABCDE-ABCDE-2JGH3M":{"refid":null,"userref":0,"status":"open","opentm":1666646615.668582,"starttm":0,"expiretm":0,"descr":{"pair":"LINKEUR","type":"sell","ordertype":"limit","price":"8.30000","price2":"0","leverage":"none","order":"sell 1.00000000 LINKEUR @ limit 8.30000","close":""},"vol":"1.00000000","vol_exec":"0.00000000","cost":"0.000000","fee":"0.000000","price":"0.000000","stopprice":"0.000000","limitprice":"0.000000","misc":"","oflags":"fciq"},"ABCDE-623OO-ABCDE":{"refid":null,"userref":0,"status":"open","opentm":1666646572.9639573,"starttm":0,"expiretm":0,"descr":{"pair":"LINKEUR","type":"sell","ordertype":"limit","price":"8.20000","price2":"0","leverage":"none","order":"sell 1.00000000 LINKEUR @ limit 8.20000","close":""},"vol":"1.00000000","vol_exec":"0.00000000","cost":"0.000000","fee":"0.000000","price":"0.000000","stopprice":"0.000000","limitprice":"0.000000","misc":"","oflags":"fciq"},"ABCDE-4EHBG-ABCDE":{"refid":null,"userref":0,"status":"open","opentm":1666646537.55891,"starttm":0,"expiretm":0,"descr":{"pair":"LINKEUR","type":"sell","ordertype":"limit","price":"8.10000","price2":"0","leverage":"none","order":"sell 1.00000000 LINKEUR @ limit 8.10000","close":""},"vol":"1.00000000","vol_exec":"0.00000000","cost":"0.000000","fee":"0.000000","price":"0.000000","stopprice":"0.000000","limitprice":"0.000000","misc":"","oflags":"fciq"},"ABCDE-SPHMF-ABCDE":{"refid":null,"link_id":"ABCDE-YWJVW-ABCDE","userref":0,"status":"open","opentm":1666646453.770994,"starttm":0,"expiretm":0,"descr":{"pair":"LINKEUR","type":"sell","ordertype":"limit","price":"7.99900","price2":"0","leverage":"none","order":"sell 1.00000000 LINKEUR @ limit 7.99900","close":""},"vol":"1.00000000","vol_exec":"0.00000000","cost":"0.000000","fee":"0.000000","price":"0.000000","stopprice":"0.000000","limitprice":"0.000000","misc":"","oflags":"fcib"},"ABCDE-RVWQO-ABCDE":{"refid":null,"userref":0,"status":"open","opentm":1663311467.0131989,"starttm":0,"expiretm":0,"descr":{"pair":"ETHWUSD","type":"sell","ordertype":"limit","price":"1000.000","price2":"0","leverage":"none","order":"sell 357.00000000 ETHWUSD @ limit 1000.000","close":""},"vol":"357.00000000","vol_exec":"0.00000000","cost":"0.0000000","fee":"0.0000000","price":"0.0000000","stopprice":"0.0000000","limitprice":"0.0000000","misc":"","oflags":"fcib"}}}}

df=pd.DataFrame(returned_json['result'])['open'].apply(pd.Series).rename(columns={'price':'price_main'})
df=df.join(df['descr'].apply(pd.Series))
print(df)
'''
                      refid userref status             opentm     starttm   expiretm    descr   vol vol_exec    cost    fee price_main  stopprice   limitprice  misc    oflags  link_id pair    type    ordertype   price   price2  leverage    order   close
ABCDE-4EHBG-ABCDE       0   open    1666646537.55891    0   0   {'pair': 'LINKEUR', 'type': 'sell', 'ordertype': 'limit', 'price': '8.10000', 'price2': '0', 'leverage': 'none', 'order': 'sell 1.00000000 LINKEUR @ limit 8.10000', 'close': ''}   1.00000000  0.00000000  0.000000    0.000000    0.000000    0.000000    0.000000        fciq        LINKEUR sell    limit   8.10000 0   none    sell 1.00000000 LINKEUR @ limit 8.10000 
ABCDE-623OO-ABCDE       0   open    1666646572.9639573  0   0   {'pair': 'LINKEUR', 'type': 'sell', 'ordertype': 'limit', 'price': '8.20000', 'price2': '0', 'leverage': 'none', 'order': 'sell 1.00000000 LINKEUR @ limit 8.20000', 'close': ''}   1.00000000  0.00000000  0.000000    0.000000    0.000000    0.000000    0.000000        fciq        LINKEUR sell    limit   8.20000 0   none    sell 1.00000000 LINKEUR @ limit 8.20000 
ABCDE-ABCDE-2JGH3M      0   open    1666646615.668582   0   0   {'pair': 'LINKEUR', 'type': 'sell', 'ordertype': 'limit', 'price': '8.30000', 'price2': '0', 'leverage': 'none', 'order': 'sell 1.00000000 LINKEUR @ limit 8.30000', 'close': ''}   1.00000000  0.00000000  0.000000    0.000000    0.000000    0.000000    0.000000        fciq        LINKEUR sell    limit   8.30000 0   none    sell 1.00000000 LINKEUR @ limit 8.30000 
ABCDE-AHKEK-ABCDE       0   open    1666683548.8137474  0   0   {'pair': 'LINKEUR', 'type': 'sell', 'ordertype': 'limit', 'price': '8.60000', 'price2': '0', 'leverage': 'none', 'order': 'sell 1.00000000 LINKEUR @ limit 8.60000', 'close': ''}   1.00000000  0.00000000  0.000000    0.000000    0.000000    0.000000    0.000000        fcib        LINKEUR sell    limit   8.60000 0   none    sell 1.00000000 LINKEUR @ limit 8.60000 
ABCDE-AWSCG-ABCDE       0   open    1666683580.8848298  0   0   {'pair': 'LINKEUR', 'type': 'sell', 'ordertype': 'limit', 'price': '8.70000', 'price2': '0', 'leverage': 'none', 'order': 'sell 1.00000000 LINKEUR @ limit 8.70000', 'close': ''}   1.00000000  0.00000000  0.000000    0.000000    0.000000    0.000000    0.000000        fcib        LINKEUR sell    limit   8.70000 0   none    sell 1.00000000 LINKEUR @ limit 8.70000 
ABCDE-POQX7-ABCDE       0   open    1666683517.207053   0   0   {'pair': 'LINKEUR', 'type': 'sell', 'ordertype': 'limit', 'price': '8.50000', 'price2': '0', 'leverage': 'none', 'order': 'sell 1.00000000 LINKEUR @ limit 8.50000', 'close': ''}   1.00000000  0.00000000  0.000000    0.000000    0.000000    0.000000    0.000000        fcib        LINKEUR sell    limit   8.50000 0   none    sell 1.00000000 LINKEUR @ limit 8.50000 
ABCDE-RVWQO-ABCDE       0   open    1663311467.0131989  0   0   {'pair': 'ETHWUSD', 'type': 'sell', 'ordertype': 'limit', 'price': '1000.000', 'price2': '0', 'leverage': 'none', 'order': 'sell 357.00000000 ETHWUSD @ limit 1000.000', 'close': ''}   357.00000000    0.00000000  0.0000000   0.0000000   0.0000000   0.0000000   0.0000000       fcib        ETHWUSD sell    limit   1000.000    0   none    sell 357.00000000 ETHWUSD @ limit 1000.000  
ABCDE-SPHMF-ABCDE       0   open    1666646453.770994   0   0   {'pair': 'LINKEUR', 'type': 'sell', 'ordertype': 'limit', 'price': '7.99900', 'price2': '0', 'leverage': 'none', 'order': 'sell 1.00000000 LINKEUR @ limit 7.99900', 'close': ''}   1.00000000  0.00000000  0.000000    0.000000    0.000000    0.000000    0.000000        fcib    ABCDE-YWJVW-ABCDE   LINKEUR sell    limit   7.99900 0   none    sell 1.00000000 LINKEUR @ limit 7.99900 
ABCDE-UADOV-ABCDE       0   open    1666683490.4299977  0   0   {'pair': 'LINKEUR', 'type': 'sell', 'ordertype': 'limit', 'price': '8.40000', 'price2': '0', 'leverage': 'none', 'order': 'sell 1.00000000 LINKEUR @ limit 8.40000', 'close': ''}   1.00000000  0.00000000  0.000000    0.000000    0.000000    0.000000    0.000000        fcib        LINKEUR sell    limit   8.40000 0   none    sell 1.00000000 LINKEUR @ limit 8.40000 

'''

相关问题