简体   繁体   English

如何使用 python 从 json 文件中获取特定数据

[英]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.这使用 JSON 格式将 dict g序列化为字符串。 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.实际上,您根本不需要import json ,因为pybit已经解析了来自 HTTP 请求的 JSON 响应,因此您无需执行任何操作。 Instead, you should use g directly: g['USDT'] .相反,您应该直接使用gg['USDT'] Better yet, just rename g to data :更好的是,只需将g重命名为data

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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