我正在建造一个股票交易机器人,它可以根据我的策略为我交易股票。现在我只是在设置选股功能。
当我尝试"TypeError: the JSON object must be str, bytes or bytearray, not 'dict'"
或json.dumps时,我一直得到json.loads或json.dumps
我只想从json的回复中提取"lastPrice“。在这种情况下,lastPrice应该是135.87
,这就是我得到的:
data = content.json()
Json数据(从代理api中提取)
{'WBC': {'assetType': 'EQUITY', 'assetMainType': 'EQUITY', 'cusip': '92927K102', 'symbol': 'WBC', 'description': 'Wabco Holdings Inc. Common Stock', 'bidPrice': 135.76, 'bidSize': 100, 'bidId': 'K', 'askPrice': 136.1, 'askSize': 2000, 'askId': 'P', 'lastPrice': 135.87, 'lastSize': 66100, 'lastId': 'N', 'openPrice': 135.77, 'highPrice': 135.95, 'lowPrice': 135.77, 'bidTick': ' ', 'closePrice': 135.79, 'netChange': 0.08, 'totalVolume': 482625, 'quoteTimeInLong': 1579903320193, 'tradeTimeInLong': 1579908600006, 'mark': 135.87, 'exchange': 'n', 'exchangeName': 'NYSE', 'marginable': True, 'shortable': True, 'volatility': 0.006, 'digits': 2, '52WkHigh': 146.675, '52WkLow': 108.09, 'nAV': 0.0, 'peRatio': 21.29, 'divAmount': 0.0, 'divYield': 0.0, 'divDate': '', 'securityStatus': 'Normal', 'regularMarketLastPrice': 135.87, 'regularMarketLastSize': 661, 'regularMarketNetChange': 0.08, 'regularMarketTradeTimeInLong': 1579908600006, 'netPercentChangeInDouble': 0.0589, 'markChangeInDouble': 0.08, 'markPercentChangeInDouble': 0.0589, 'regularMarketPercentChangeInDouble': 0.0589, 'delayed': True}}
只打印135.87
val = json.loads(data)
print(val['lastPrice'])
发布于 2020-01-25 00:10:01
如果您有“WBC”映射到整个字典,请尝试:
val['WBC']['lastPrice']
https://stackoverflow.com/questions/59905161
复制相似问题