首页 > 解决方案 > 访问 Python 数组中的数据

问题描述

我正在提取库存数据,我需要访问“callExpDateMap”中出现的第一个选项。但是,我不确定。

我试图通过调用访问第一个选项的数据:

      xyz = json.dumps(data, indent=4)
        print(xyz)


{
    "symbol": "GOEV",
    "status": "SUCCESS",
    "underlying": null,
    "strategy": "SINGLE",
    "interval": 0.0,
    "isDelayed": true,
    "isIndex": false,
    "interestRate": 0.1,
    "underlyingPrice": 10.969999999999999,
    "volatility": 29.0,
    "daysToExpiration": 0.0,
    "numberOfContracts": 5,
    "putExpDateMap": {},
    "callExpDateMap": {
        "2021-06-18:3": {
            "11.5": [
                {
                    "putCall": "CALL",
                    "symbol": "GOEV_061821C11.5",
                    "description": "GOEV Jun 18 2021 11.5 Call",
                    "exchangeName": "OPR",
                    "bid": 0.6,
                    "ask": 0.7,
                    "last": 0.6,
                    "mark": 0.65,
                    "bidSize": 4,
                    "askSize": 138,
                    "bidAskSize": "4X138",
                    "lastSize": 0,
                    "highPrice": 1.0,
                    "lowPrice": 0.1,
                    "openPrice": 0.0,
                    "closePrice": 0.1,
                    "totalVolume": 4416,
                    "tradeDate": null,
                    "tradeTimeInLong": 1623787194008,
                    "quoteTimeInLong": 1623787198521,
                    "netChange": 0.5,
                    "volatility": 195.881,
                    "delta": 0.448,
                    "gamma": 0.183,
                    "theta": -0.111,
                    "vega": 0.004,
                    "rho": 0.0,
                    "openInterest": 100,
                    "timeValue": 0.6,
                    "theoreticalOptionValue": 0.654,
                    "theoreticalVolatility": 29.0,
                    "optionDeliverablesList": null,
                    "strikePrice": 11.5,
                    "expirationDate": 1624046400000,
                    "daysToExpiration": 3,
                    "expirationType": "R",
                    "lastTradingDay": 1624060800000,
                    "multiplier": 100.0,
                    "settlementType": " ",
                    "deliverableNote": "",
                    "isIndexOption": null,
                    "percentChange": 500.0,
                    "markChange": 0.55,
                    "markPercentChange": 554.2,
                    "nonStandard": false,
                    "inTheMoney": false,
                    "mini": false
                }
            ]
        },
     

要访问第一项,我打电话给:

   print(xyz['callExpDateMap'][0])

但是,那行不通。如何将数组中的第一项传递给单独的变量?

标签: pythonjson

解决方案


也许这会对你有所帮助

callExpDateMap = list(data['callExpDateMap'].values())
print(callExpDateMap[0])

推荐阅读