【发布时间】:2019-05-06 16:03:29
【问题描述】:
我尝试从中解析 json:
{
"lastUpdateId": 78772216,
"bids": [
[
"0.00000421",
"133090.00000000"
],
[
"0.00000420",
"345637.00000000"
],
[
"0.00000419",
"84680.00000000"
],
[
"0.00000418",
"127899.00000000"
],
[
"0.00000417",
"175359.00000000"
]
],
"asks": [
[
"0.00000422",
"324731.00000000"
],
[
"0.00000423",
"323497.00000000"
],
[
"0.00000424",
"86010.00000000"
],
[
"0.00000425",
"207321.00000000"
],
[
"0.00000426",
"161378.00000000"
]
]
}
但总是有一些问题。
我试试这个:
from binance.client import Client
import json
api_key = "..."
api_secret = "..."
client = Client(api_key, api_secret)
depth = client.get_order_book(symbol='QKCBTC', limit=5)
file = json.dumps(depth, indent=2)
for i in file["asks"]:
print(i[1])
我尝试转储、加载、加载而不是“转储”。
错误:
转储 - 类型错误:字符串索引必须是整数;
loads - raise TypeError(f'JSON对象必须是str, bytes or bytearray, ' TypeError: JSON对象必须是str, bytes or bytearray, not dict;
load - AttributeError: 'dict' object has no attribute 'read';
dump - 类型错误:dump() 缺少 1 个必需的位置参数:'fp';
感谢您的解决方案。
【问题讨论】:
-
错误告诉你你有一本字典——你不需要从 json 反序列化,因为这已经完成了。
-
您不需要使用
json.dumps,因为您已经有了字典。调用print(depth['asks'])将打印数字列表。 -
谢谢,你解决了我的问题;D