【问题标题】:Accessing data in the json array object访问 json 数组对象中的数据
【发布时间】:2020-10-01 19:21:25
【问题描述】:

我得到了一个数组结构作为 json。我需要访问这个数组结构中的一些元素。

'asset': [{'asset': 'USDT',

我需要访问其中的 walletBalance 对象。 我该怎么做?

 def account_balance(webhook_data):
     account_data = client.futures_account()
     temp_account_data = json.dumps(account_data)
     test = json.loads(temp_account_data)

     for sym in test:
         print(sym['assets'])
     return account_balance

使用上面的代码sn-p,对象以json的形式出现,但我无法访问“资产”内容

{'feeTier': 0, 
'canTrade': True, 
'canDeposit': True, 
'canWithdraw': True, 
'updateTime': 0, 
'totalInitialMargin': '0.00000000', 
'totalMaintMargin': '0.00000000', 
'totalWalletBalance': '32.51609961', 
'totalUnrealizedProfit': '0.00000000', 
'totalMarginBalance': '32.51609961', 
'totalPositionInitialMargin': '0.00000000', 
'totalOpenOrderInitialMargin': '0.00000000', 
'maxWithdrawAmount': '32.51609961', 

'assets': 

[
{'asset': 'USDT', 
'walletBalance': '32.51609961', 
'unrealizedProfit': '0.00000000', 
'marginBalance': '32.51609961', 
'maintMargin': '0.00000000', 
'initialMargin': '0.00000000', 
'positionInitialMargin': '0.00000000', 
'openOrderInitialMargin': '0.00000000', 
'maxWithdrawAmount': '32.51609961'
},

 {'asset': 'BNB', 'walletBalance': '0.00000000', 
'unrealizedProfit': '0.00000000', 'marginBalance': '0.00000000', 
'maintMargin': '0.00000000', 'initialMargin': '0.00000000', 
'positionInitialMargin': '0.00000000', 
'openOrderInitialMargin': '0.00000000',
'maxWithdrawAmount': '0.00000000'}], 



'positions': [
{'symbol': 'EOSUSDT', 'initialMargin': '0', 'maintMargin': '0', 'unrealizedProfit': '0.00000000', 'positionInitialMargin': '0', 'openOrderInitialMargin': '0', 'leverage': '10', 'isolated': False, 'entryPrice': '0.0000', 'maxNotional': '1000000', 'positionSide': 'BOTH'}, 

{'symbol': 'SUSHIUSDT', 'initialMargin': '0', 'maintMargin': '0', 'unrealizedProfit': '0.00000000', 'positionInitialMargin': '0', 'openOrderInitialMargin': '0', 'leverage': '20', 'isolated': False, 'entryPrice': '0.0000', 'maxNotional': '25000', 'positionSide': 'BOTH'}
]}  

【问题讨论】:

  • test['assets'][0]['walletBalance']
  • 非常感谢我的朋友。成功了,你能写出答案吗?让我们确认

标签: python json parsing flask


【解决方案1】:

请注意,可能有多个 asste,因此请遍历它们:

for asset in json_data.get('assets', []):
    print(asset.get('walletBalance'))

【讨论】:

    【解决方案2】:

    你可以使用列表推导

    res = [x.get('walletBalance') for x in test['assets']]
    print(res)
    

    输出:

    ['32.51609961', '0.00000000']
    

    【讨论】:

      【解决方案3】:

      看来您正在从调用 client.futures_account() 中获取字典 .如果是,则不需要转储和加载。 你可以这样做:

       def account_balance(webhook_data):
           account_data = client.futures_account()
           return [x.get('walletBalance') for x in account_data['assets']]
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多