首页 > 解决方案 > 从python中的嵌套元组中获取值

问题描述

我有一个来自 api 的 json 响应,我需要从以下位置获取“askPrice”值:

dict_items(
    [
        ('SPY', 
            {
                 'assetType': 'ETF', 
                 'assetMainType': 'EQUITY', 
                 'cusip': '78462F103', 
                 'assetSubType': 'ETF', 
                 'symbol': 'SPY', 
                 'description': 'SPDR S&P 500', 
                 'bidPrice': 435.31, 
                 'bidSize': 2100, 
                 'bidId': 'T', 
                 'askPrice': 435.33, 
                 'askSize': 200, 
                 'askId': 'P', 
                 'lastPrice': 435.32, 
                 'lastSize': 0, 
                 ... , 
                 'delayed': True, 
                 'realtimeEntitled': False
             }
         )
     ]
 )

我的代码是:

quote = c.get_quotes("SPY")
resp = quote.json().items()
print(pd.json_normalize(resp['SPY']['AskPrice'],level=2))

但是python解释器显示错误:

TypeError:“dict_items”对象不可下标

我为这个简单的问题道歉。

标签: pythonjsondictionary

解决方案


推荐阅读