JSON同时支持多个查询

JSON同时支持多个查询,json,python-3.x,error-handling,Json,Python 3.x,Error Handling,我有一个python方法可以捕获以下JSON信息: {'info': {'orderId': 5913316, 'clientOrderId': 'ME_dot', 'origQty': '0.02000000', 'trade': 'xxx', 'updateTime': xxx, 'side': 'BUY', 'timeInForce': 'GTC', 'status': 'FILLED', 'stopPrice': '0.0', 'time': xxx, 'isWorking': True

我有一个python方法可以捕获以下JSON信息:

{'info': {'orderId': 5913316, 'clientOrderId': 'ME_dot', 'origQty': '0.02000000', 'trade': 'xxx', 'updateTime': xxx, 'side': 'BUY', 'timeInForce': 'GTC', 'status': 'FILLED', 'stopPrice': '0.0', 'time': xxx, 'isWorking': True, 'type': 'LIMIT', 'price': '506.10887700', 'executedQty': '0.02000000'}, 'lastTradeTimestamp': None, 'remaining': 0.0, 'fee': None, 'timestamp': x, 'symbol': 'xxx', 'trades': None, 'datetime': 'xxx, 'price': 0.108877, 'amount': 0.02, 'cost': 0.00217754, 'status': 'closed', 'type': 'limit', 'id': '59139516', 'filled': 0.02, 'side': 'buy'}
从中,我只需要捕获
clientOrderId
executedQty
,因此我尝试以下操作:

id, q  =  (exchange.order_status(trading )['info']['clientOrderId'], ['info']['executedQty'])
这就带来了这个问题:
TypeError:list索引必须是整数,而不是str


那么,如何在同一行代码中捕获值呢?

首先,您需要将JSON字符串转换为JSON对象。但是问题中的
json
格式不正确,无法直接转换。所以

将您的JSON正确地封装在
中。这是一个格式正确的

order_status = '{"info": {"orderId": 5913316, "clientOrderId": "ME_dot", "origQty": "0.02000000", "trade": "xxx", "updateTime": "xxx", "side": "BUY", "timeInForce": "GTC", "status": "FILLED", "stopPrice": "0.0", "time": "xxx", "isWorking": "True", "type": "LIMIT", "price": "506.10887700", "executedQty": "0.02000000"}, "lastTradeTimestamp": "None", "remaining": 0.0, "fee": "None", "timestamp": "x", "symbol": "xxx", "trades": "None", "datetime": "xxx", "price": 0.108877, "amount": 0.02, "cost": 0.00217754, "status": "closed", "type": "limit", "id": "59139516", "filled": 0.02, "side": "buy"}'
一旦完成了,你就可以

import json
order_status_json = json.loads(order_status) 
这将把字符串转换成
JSON
对象,现在您可以轻松地查询值

像这样,

p, q  =  (order_status_json['info']['clientOrderId'],order_status_json['info']['executedQty'])
返回

>>> p

'ME_dot'
>>> q

'0.02000000'

@Esteban G.Gutierrez接受答案,如果它有助于您将其标记为已解决。