简体   繁体   中英

how to get specific data from json file using python

this is my code below

from pybit import HTTP
    import json
    
    session = HTTP("https://api.bybit.com",api_key=xxxx, api_secret=xxxx)
    g=session.get_wallet_balance(coin="USDT")
    data=json.dumps(g,indent=4)
    print(data)
    for result in data ['USDT']:
        print(["equity"])

keep getting type error like this

    for result in data ['USDT']:
TypeError: string indices must be integers
    data=json.dumps(g,indent=4)

This serializes the dict g into a string using the JSON format. Don't do that!

In fact, you don't need import json at all because pybit has already parsed the JSON response from the HTTP request, so you don't need to do anything. Instead, you should use g directly: g['USDT'] . Better yet, just rename g to data :

data = session.get_wallet_balance(coin="USDT")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM