Python 具有子字符串的API JSON字典返回TypeError:字符串索引必须是整数

Python 具有子字符串的API JSON字典返回TypeError:字符串索引必须是整数,python,json,Python,Json,我使用以下代码从API中提取json提要: import requests import json url = "https://apidojo-yahoo-finance-v1.p.rapidapi.com/stock/v2/get-holders" querystring = {"symbol":"zwu.to"} headers = { 'x-rapidapi-key': "12345",

我使用以下代码从API中提取json提要:

import requests
import json

url = "https://apidojo-yahoo-finance-v1.p.rapidapi.com/stock/v2/get-holders"

querystring = {"symbol":"zwu.to"}

headers = {
    'x-rapidapi-key': "12345",
    'x-rapidapi-host': "67890"
    }

response = requests.request("GET", url, headers=headers, params=querystring)
source = response.json()

print("Source is: ",type(source))

for item in source["fundProfile"]["feesExpensesInvestment"]:
    fees = item["annualReportExpenseRatio"]["fmt"]
    print("Items are: ", type(item))
但是,当我试图获得费用的价值时,我陷入了:

TypeError:字符串索引必须是整数

我尝试使用json.dumpsjson.loads将子字符串转换为字典,但没有成功

以下是我打印时得到的类型:

资料来源是: 项目包括:

以下是源对象的结构:

{"defaultKeyStatistics":{"annualHoldingsTurnover":{},"enterpriseToRevenue":{},"beta3Year":{"raw":0.75,"fmt":"0.75"},"profitMargins":{},"enterpriseToEbitda":{},"52WeekChange":{},"morningStarRiskRating":{},"forwardEps":{},"revenueQuarterlyGrowth":{},"fundInceptionDate":{"raw":1319068800,"fmt":"2011-10-20"},"annualReportExpenseRatio":{},"totalAssets":{"raw":965995584,"fmt":"966M","longFmt":"965,995,584"},"bookValue":{},"fundFamily":"BMO Asset Management Inc","lastFiscalYearEnd":{},"netIncomeToCommon":{},"trailingEps":{},"lastDividendValue":{},"SandP52WeekChange":{},"priceToBook":{},"nextFiscalYearEnd":{},"yield":{"raw":0.0794,"fmt":"7.94%"},"mostRecentQuarter":{},"enterpriseValue":{},"priceHint":{"raw":2,"fmt":"2","longFmt":"2"},"threeYearAverageReturn":{"raw":0.0477,"fmt":"4.77%"},"lastSplitDate":{},"lastSplitFactor":null,"legalType":"Exchange Traded Fund","lastDividendDate":{},"morningStarOverallRating":{},"earningsQuarterlyGrowth":{},"priceToSalesTrailing12Months":{},"pegRatio":{},"ytdReturn":{"raw":0.0357,"fmt":"3.57%"},"forwardPE":{},"maxAge":1,"lastCapGain":{},"category":null,"fiveYearAverageReturn":{"raw":0.080699995,"fmt":"8.07%"}},"fundProfile":{"initInvestment":{},"family":"BMO Asset Management Inc","categoryName":null,"initAipInvestment":{},"subseqIraInvestment":{},"brokerages":[],"managementInfo":{"managerName":null,"managerBio":null,"startdate":{}},"subseqInvestment":{},"legalType":"Exchange Traded Fund","styleBoxUrl":"http://us.i1.yimg.com/us.yimg.com/i/fi/3_0stylelargeeq1.gif","feesExpensesInvestment":{"annualHoldingsTurnover":{"raw":0.3132,"fmt":"31.32%"},"frontEndSalesLoad":{},"annualReportExpenseRatio":{"raw":0.0064999997,"fmt":"0.65%"},"netExpRatio":{},"projectionValues":{},"grossExpRatio":{},"deferredSalesLoad":{},"totalNetAssets":{"raw":29117.04,"fmt":"29,117.04"},"twelveBOne":{}},"maxAge":1,"feesExpensesInvestmentCat":{"annualReportExpenseRatio":{},"frontEndSalesLoad":{},"deferredSalesLoad":{},"twelveBOne":{},"annualHoldingsTurnover":{},"totalNetAssets":{"raw":29117.04,"fmt":"29,117.04"},"projectionValuesCat":{}},"initIraInvestment":{},"subseqAipInvestment":{}},"financialsTemplate":{},"topHoldings":{"preferredPosition":{},"bondPosition":{"raw":0,"fmt":"0.00%"},"convertiblePosition":{},"sectorWeightings":[{"realestate":{"raw":0,"fmt":"0.00%"}},{"consumer_cyclical":{"raw":0,"fmt":"0.00%"}},{"basic_materials":{"raw":0,"fmt":"0.00%"}},{"consumer_defensive":{"raw":0,"fmt":"0.00%"}},{"technology":{"raw":0,"fmt":"0.00%"}},{"communication_services":{"raw":0.2491,"fmt":"24.91%"}},{"financial_services":{"raw":0.0021,"fmt":"0.21%"}},{"utilities":{"raw":0.5384,"fmt":"53.84%"}},{"industrials":{"raw":0,"fmt":"0.00%"}},{"energy":{"raw":0.21040002,"fmt":"21.04%"}},{"healthcare":{"raw":0,"fmt":"0.00%"}}],"holdings":[{"symbol":"PPL.TO","holdingName":"Pembina Pipeline Corp","holdingPercent":{"raw":0.0588,"fmt":"5.88%"}},{"symbol":"ZUT.TO","holdingName":"BMO Equal Weight Utilities ETF","holdingPercent":{"raw":0.0574,"fmt":"5.74%"}},{"symbol":"T.TO","holdingName":"TELUS Corp","holdingPercent":{"raw":0.053600002,"fmt":"5.36%"}},{"symbol":"ENB.TO","holdingName":"Enbridge Inc","holdingPercent":{"raw":0.052199997,"fmt":"5.22%"}},{"symbol":"FTS.TO","holdingName":"Fortis Inc","holdingPercent":{"raw":0.0519,"fmt":"5.19%"}},{"symbol":"EMA.TO","holdingName":"Emera Inc","holdingPercent":{"raw":0.051,"fmt":"5.10%"}},{"symbol":"TRP.TO","holdingName":"TC Energy Corp","holdingPercent":{"raw":0.0509,"fmt":"5.09%"}},{"symbol":"BCE.TO","holdingName":"BCE Inc","holdingPercent":{"raw":0.0502,"fmt":"5.02%"}},{"symbol":"EXC","holdingName":"Exelon Corp","holdingPercent":{"raw":0.049099997,"fmt":"4.91%"}},{"symbol":"VZ","holdingName":"Verizon Communications Inc","holdingPercent":{"raw":0.049000002,"fmt":"4.90%"}}],"bondHoldings":{"maturity":{},"duration":{},"creditQuality":{},"maturityCat":{},"durationCat":{},"creditQualityCat":{}},"bondRatings":[{"bb":{"raw":0,"fmt":"0.00%"}},{"aa":{"raw":0,"fmt":"0.00%"}},{"aaa":{"raw":0,"fmt":"0.00%"}},{"a":{"raw":0,"fmt":"0.00%"}},{"other":{"raw":0,"fmt":"0.00%"}},{"b":{"raw":0,"fmt":"0.00%"}},{"bbb":{"raw":0,"fmt":"0.00%"}},{"below_b":{"raw":0,"fmt":"0.00%"}},{"us_government":{"raw":0,"fmt":"0.00%"}}],"equityHoldings":{"priceToCashflow":{"raw":6.6,"fmt":"6.6"},"priceToSales":{"raw":1.76,"fmt":"1.76"},"priceToBookCat":{},"priceToEarningsCat":{},"medianMarketCapCat":{},"threeYearEarningsGrowthCat":{},"threeYearEarningsGrowth":{},"medianMarketCap":{},"priceToEarnings":{"raw":16.15,"fmt":"16.15"},"priceToBook":{"raw":1.66,"fmt":"1.66"},"priceToSalesCat":{},"priceToCashflowCat":{}},"otherPosition":{},"maxAge":1,"cashPosition":{},"stockPosition":{"raw":0.984,"fmt":"98.40%"}},"price":{"quoteSourceName":"Delayed Quote","regularMarketOpen":{"raw":12.2,"fmt":"12.20"},"averageDailyVolume3Month":{"raw":157771,"fmt":"157.77k","longFmt":"157,771"},"exchange":"TOR","regularMarketTime":1611953982,"volume24Hr":{},"regularMarketDayHigh":{"raw":12.22,"fmt":"12.22"},"shortName":"BMO COVERED CALL UTILITIES ETF","averageDailyVolume10Day":{"raw":179837,"fmt":"179.84k","longFmt":"179,837"},"longName":"BMO Covered Call Utilities ETF","regularMarketChange":{"raw":-0.05000019,"fmt":"-0.05"},"currencySymbol":"$","regularMarketPreviousClose":{"raw":12.22,"fmt":"12.22"},"preMarketPrice":{},"exchangeDataDelayedBy":15,"toCurrency":null,"postMarketChange":{},"postMarketPrice":{},"exchangeName":"Toronto","preMarketChange":{},"circulatingSupply":{},"regularMarketDayLow":{"raw":12.09,"fmt":"12.09"},"priceHint":{"raw":2,"fmt":"2","longFmt":"2"},"currency":"CAD","regularMarketPrice":{"raw":12.17,"fmt":"12.17"},"regularMarketVolume":{"raw":268730,"fmt":"268.73k","longFmt":"268,730.00"},"lastMarket":null,"regularMarketSource":"FREE_REALTIME","openInterest":{},"marketState":"POSTPOST","underlyingSymbol":null,"marketCap":{},"quoteType":"ETF","volumeAllCurrencies":{},"strikePrice":{},"symbol":"ZWU.TO","maxAge":1,"fromCurrency":null,"regularMarketChangePercent":{"raw":-0.0040916684,"fmt":"-0.41%"}},"fundPerformance":{"trailingReturns":{"threeMonth":{"raw":0.045300003,"fmt":"4.53%"},"oneYear":{"raw":-0.0705,"fmt":"-7.05%"},"ytd":{"raw":-0.0705,"fmt":"-7.05%"},"lastBearMkt":{"raw":0,"fmt":"0.00%"},"lastBullMkt":{"raw":0,"fmt":"0.00%"},"fiveYear":{"raw":0.0651,"fmt":"6.51%"},"asOfDate":{"raw":1609372800,"fmt":"2020-12-31"},"tenYear":{"raw":0,"fmt":"0.00%"},"oneMonth":{"raw":-0.0082,"fmt":"-0.82%"},"threeYear":{"raw":0.026600001,"fmt":"2.66%"}},"performanceOverview":{"bestThreeYrTotalReturn":{},"morningStarReturnRating":{},"ytdReturnPct":{"raw":0.0357,"fmt":"3.57%"},"fiveYrAvgReturnPct":{},"numYearsUp":{},"worstOneYrTotalReturn":{},"oneYearTotalReturn":{"raw":-0.0591,"fmt":"-5.91%"},"numYearsDown":{},"asOfDate":{"raw":1610668800,"fmt":"2021-01-15"},"worstThreeYrTotalReturn":{},"bestOneYrTotalReturn":{},"threeYearTotalReturn":{"raw":0.0477,"fmt":"4.77%"}},"riskOverviewStatistics":{"riskRating":{},"riskStatistics":[{"year":"5y","alpha":{"raw":0.28,"fmt":"0.28"},"beta":{"raw":0.57,"fmt":"0.57"},"meanAnnualReturn":{"raw":0.58,"fmt":"0.58"},"rSquared":{"raw":34.7,"fmt":"34.7"},"stdDev":{"raw":11.43,"fmt":"11.43"},"sharpeRatio":{"raw":0.52,"fmt":"0.52"},"treynorRatio":{"raw":9.65,"fmt":"9.65"}},{"year":"3y","alpha":{"raw":-5.12,"fmt":"-5.12"},"beta":{"raw":0.75,"fmt":"0.75"},"meanAnnualReturn":{"raw":0.3,"fmt":"0.3"},"rSquared":{"raw":51.44,"fmt":"51.44"},"stdDev":{"raw":13.77,"fmt":"13.77"},"sharpeRatio":{"raw":0.17,"fmt":"0.17"},"treynorRatio":{"raw":1.93,"fmt":"1.93"}}]},"riskOverviewStatisticsCat":{"riskStatisticsCat":[]},"performanceOverviewCat":{"bestThreeYrTotalReturn":{},"morningStarReturnRating":{},"ytdReturnPct":{},"fiveYrAvgReturnPct":{},"numYearsUp":{},"worstOneYrTotalReturn":{},"numYearsDown":{},"asOfDate":{},"worstThreeYrTotalReturn":{},"bestOneYrTotalReturn":{}},"trailingReturnsCat":{"threeMonth":{"raw":0,"fmt":"0.00%"},"oneYear":{"raw":0,"fmt":"0.00%"},"ytd":{"raw":0,"fmt":"0.00%"},"lastBearMkt":{"raw":0,"fmt":"0.00%"},"lastBullMkt":{"raw":0,"fmt":"0.00%"},"fiveYear":{"raw":0,"fmt":"0.00%"},"tenYear":{"raw":0,"fmt":"0.00%"},"oneMonth":{"raw":0,"fmt":"0.00%"},"threeYear":{"raw":0,"fmt":"0.00%"}},"maxAge":1,"pastQuarterlyReturns":{"returns":[]},"trailingReturnsNav":{"ytd":{"raw":-0.0713,"fmt":"-7.13%"},"oneMonth":{"raw":-0.0089,"fmt":"-0.89%"},"threeMonth":{"raw":0.0468,"fmt":"4.68%"},"oneYear":{"raw":-0.0713,"fmt":"-7.13%"},"threeYear":{"raw":0.027,"fmt":"2.70%"},"fiveYear":{"raw":0.0647,"fmt":"6.47%"},"tenYear":{"raw":0,"fmt":"0.00%"}},"annualTotalReturns":{"returns":[{"year":"2020","annualValue":{"raw":-0.071293205,"fmt":"-7.13%"}},{"year":"2019","annualValue":{"raw":0.2344374,"fmt":"23.44%"}},{"year":"2018","annualValue":{"raw":-0.0550568,"fmt":"-5.51%"}},{"year":"2017","annualValue":{"raw":0.056948,"fmt":"5.69%"}},{"year":"2016","annualValue":{"raw":0.1951471,"fmt":"19.51%"}},{"year":"2015","annualValue":{"raw":-0.1524073,"fmt":"-15.24%"}},{"year":"2014","annualValue":{"raw":0.1439003,"fmt":"14.39%"}},{"year":"2013","annualValue":{"raw":0.0162982,"fmt":"1.63%"}},{"year":"2012","annualValue":{"raw":0.0387747,"fmt":"3.88%"}}],"returnsCat":[]}},"quoteType":{"exchange":"TOR","shortName":"BMO COVERED CALL UTILITIES ETF","longName":"BMO Covered Call Utilities ETF","exchangeTimezoneName":"America/Toronto","exchangeTimezoneShortName":"EST","isEsgPopulated":false,"gmtOffSetMilliseconds":"-18000000","quoteType":"ETF","symbol":"ZWU.TO","messageBoardId":"finmb_139615776","market":"ca_market"},"summaryDetail":{"previousClose":{"raw":12.22,"fmt":"12.22"},"regularMarketOpen":{"raw":12.2,"fmt":"12.20"},"twoHundredDayAverage":{"raw":12.09087,"fmt":"12.09"},"trailingAnnualDividendYield":{},"payoutRatio":{},"volume24Hr":{},"regularMarketDayHigh":{"raw":12.22,"fmt":"12.22"},"navPrice":{"raw":12.48,"fmt":"12.48"},"averageDailyVolume10Day":{"raw":179837,"fmt":"179.84k","longFmt":"179,837"},"totalAssets":{"raw":965995584,"fmt":"966M","longFmt":"965,995,584"},"regularMarketPreviousClose":{"raw":12.22,"fmt":"12.22"},"fiftyDayAverage":{"raw":12.27697,"fmt":"12.28"},"trailingAnnualDividendRate":{},"open":{"raw":12.2,"fmt":"12.20"},"toCurrency":null,"averageVolume10days":{"raw":179837,"fmt":"179.84k","longFmt":"179,837"},"expireDate":{},"yield":{"raw":0.0794,"fmt":"7.94%"},"algorithm":null,"dividendRate":{},"exDividendDate":{},"beta":{},"circulatingSupply":{},"startDate":{},"regularMarketDayLow":{"raw":12.09,"fmt":"12.09"},"priceHint":{"raw":2,"fmt":"2","longFmt":"2"},"currency":"CAD","regularMarketVolume":{"raw":268730,"fmt":"268.73k","longFmt":"268,730"},"lastMarket":null,"maxSupply":{},"openInterest":{},"marketCap":{},"volumeAllCurrencies":{},"strikePrice":{},"averageVolume":{"raw":157771,"fmt":"157.77k","longFmt":"157,771"},"priceToSalesTrailing12Months":{},"dayLow":{"raw":12.09,"fmt":"12.09"},"ask":{"raw":12.17,"fmt":"12.17"},"ytdReturn":{},"askSize":{"raw":0,"fmt":null,"longFmt":"0"},"volume":{"raw":268730,"fmt":"268.73k","longFmt":"268,730"},"fiftyTwoWeekHigh":{"raw":14.78,"fmt":"14.78"},"forwardPE":{},"maxAge":1,"fromCurrency":null,"fiveYearAvgDividendYield":{},"fiftyTwoWeekLow":{"raw":9.11,"fmt":"9.11"},"bid":{"raw":12.15,"fmt":"12.15"},"tradeable":false,"dividendYield":{},"bidSize":{"raw":0,"fmt":null,"longFmt":"0"},"dayHigh":{"raw":12.22,"fmt":"12.22"}},"symbol":"ZWU.TO","assetProfile":{"phone":"800-668-7327","longBusinessSummary":"BMO Covered Call Utilities ETF seeks to provide exposure to the performance of a portfolio of utilities companies to generate income and to provide long-term capital appreciation while mitigating downside risk through the use of covered call options.","companyOfficers":[],"maxAge":86400},"esgScores":{},"pageViews":{},"quoteData":{"ZWU.TO":{"sourceInterval":15,"quoteSourceName":"Delayed Quote","regularMarketOpen":{"raw":12.2,"fmt":"12.20"},"exchange":"TOR","regularMarketTime":{"raw":1611953982,"fmt":"3:59PM EST"},"fiftyTwoWeekRange":{"raw":"9.11 - 14.78","fmt":"9.11 - 14.78"},"regularMarketDayHigh":{"raw":12.22,"fmt":"12.22"},"shortName":"BMO COVERED CALL UTILITIES ETF","longName":"BMO Covered Call Utilities ETF","exchangeTimezoneName":"America/Toronto","regularMarketChange":{"raw":-0.05000019,"fmt":"-0.05"},"regularMarketPreviousClose":{"raw":12.22,"fmt":"12.22"},"fiftyTwoWeekHighChange":{"raw":-2.6099997,"fmt":"-2.61"},"exchangeTimezoneShortName":"EST","fiftyTwoWeekLowChange":{"raw":3.0600004,"fmt":"3.06"},"exchangeDataDelayedBy":15,"regularMarketDayLow":{"raw":12.09,"fmt":"12.09"},"priceHint":2,"currency":"CAD","regularMarketPrice":{"raw":12.17,"fmt":"12.17"},"regularMarketVolume":{"raw":268730,"fmt":"268,730","longFmt":"268,730"},"isLoading":false,"triggerable":true,"gmtOffSetMilliseconds":-18000000,"firstTradeDateMilliseconds":1327415400000,"region":"US","marketState":"POSTPOST","quoteType":"ETF","invalid":false,"symbol":"ZWU.TO","language":"en-US","fiftyTwoWeekLowChangePercent":{"raw":0.33589467,"fmt":"33.59%"},"regularMarketDayRange":{"raw":"12.09 - 12.22","fmt":"12.09 - 12.22"},"messageBoardId":"finmb_139615776","fiftyTwoWeekHigh":{"raw":14.78,"fmt":"14.78"},"fiftyTwoWeekHighChangePercent":{"raw":-0.17658997,"fmt":"-17.66%"},"uuid":"f3277957-ea5b-3bb8-9d3a-f118e537a648","market":"ca_market","fiftyTwoWeekLow":{"raw":9.11,"fmt":"9.11"},"regularMarketChangePercent":{"raw":-0.40916684,"fmt":"-0.41%"},"fullExchangeName":"Toronto","tradeable":false},"ZWB.TO":{"sourceInterval":15,"quoteSourceName":"Delayed Quote","regularMarketOpen":{"raw":17.8,"fmt":"17.80"},"exchange":"TOR","regularMarketTime":{"raw":1611953972,"fmt":"3:59PM EST"},"fiftyTwoWeekRange":{"raw":"11.44 - 18.97","fmt":"11.44 - 18.97"},"regularMarketDayHigh":{"raw":17.8,"fmt":"17.80"},"shortName":"BMO COVERED CALL CANADIAN BANKS","longName":"BMO Covered Call Canadian Banks ETF","exchangeTimezoneName":"America/Toronto","regularMarketChange":{"raw":-0.3199997,"fmt":"-0.32"},"regularMarketPreviousClose":{"raw":17.86,"fmt":"17.86"},"fiftyTwoWeekHighChange":{"raw":-1.4299984,"fmt":"-1.43"},"exchangeTimezoneShortName":"EST","fiftyTwoWeekLowChange":{"raw":6.1000013,"fmt":"6.10"},"exchangeDataDelayedBy":15,"regularMarketDayLow":{"raw":17.5,"fmt":"17.50"},"priceHint":2,"currency":"CAD","regularMarketPrice":{"raw":17.54,"fmt":"17.54"},"regularMarketVolume":{"raw":156079,"fmt":"156,079","longFmt":"156,079"},"isLoading":false,"triggerable":true,"gmtOffSetMilliseconds":-18000000,"firstTradeDateMilliseconds":1296743400000,"region":"US","marketState":"POSTPOST","quoteType":"ETF","invalid":false,"symbol":"ZWB.TO","language":"en-US","fiftyTwoWeekLowChangePercent":{"raw":0.5332169,"fmt":"53.32%"},"regularMarketDayRange":{"raw":"17.5 - 17.8","fmt":"17.50 - 17.80"},"messageBoardId":"finmb_116776236","fiftyTwoWeekHigh":{"raw":18.97,"fmt":"18.97"},"fiftyTwoWeekHighChangePercent":{"raw":-0.0753821,"fmt":"-7.54%"},"uuid":"b5f79cee-b01e-3b5b-8855-cc1b4df00852","market":"ca_market","fiftyTwoWeekLow":{"raw":11.44,"fmt":"11.44"},"regularMarketChangePercent":{"raw":-1.7917116,"fmt":"-1.79%"},"fullExchangeName":"Toronto","tradeable":false},"ZWE.TO":{"sourceInterval":15,"quoteSourceName":"Delayed Quote","regularMarketOpen":{"raw":17.78,"fmt":"17.78"},"exchange":"TOR","regularMarketTime":{"raw":1611953970,"fmt":"3:59PM EST"},"fiftyTwoWeekRange":{"raw":"13.37 - 21.25","fmt":"13.37 - 21.25"},"regularMarketDayHigh":{"raw":17.8,"fmt":"17.80"},"shortName":"BMO EUROPE HIGH DIV CC CAD HEDG","longName":"BMO Europe High Dividend Covered Call Hedged to CAD ETF","exchangeTimezoneName":"America/Toronto","regularMarketChange":{"raw":-0.29999924,"fmt":"-0.30"},"regularMarketPreviousClose":{"raw":17.92,"fmt":"17.92"},"fiftyTwoWeekHighChange":{"raw":-3.6299992,"fmt":"-3.63"},"exchangeTimezoneShortName":"EST","fiftyTwoWeekLowChange":{"raw":4.250001,"fmt":"4.25"},"exchangeDataDelayedBy":15,"regularMarketDayLow":{"raw":17.5,"fmt":"17.50"},"priceHint":2,"currency":"CAD","regularMarketPrice":{"raw":17.62,"fmt":"17.62"},"regularMarketVolume":{"raw":109589,"fmt":"109,589","longFmt":"109,589"},"isLoading":false,"triggerable":true,"gmtOffSetMilliseconds":-18000000,"firstTradeDateMilliseconds":1441978200000,"region":"US","marketState":"POSTPOST","quoteType":"ETF","invalid":false,"symbol":"ZWE.TO","language":"en-US","fiftyTwoWeekLowChangePercent":{"raw":0.31787592,"fmt":"31.79%"},"regularMarketDayRange":{"raw":"17.5 - 17.8","fmt":"17.50 - 17.80"},"messageBoardId":"finmb_306614108","fiftyTwoWeekHigh":{"raw":21.25,"fmt":"21.25"},"fiftyTwoWeekHighChangePercent":{"raw":-0.17082348,"fmt":"-17.08%"},"uuid":"f6bcc1ef-949a-3256-872d-9865d20fdac7","market":"ca_market","fiftyTwoWeekLow":{"raw":13.37,"fmt":"13.37"},"regularMarketChangePercent":{"raw":-1.6741028,"fmt":"-1.67%"},"fullExchangeName":"Toronto","tradeable":false},"ZWH.TO":{"sourceInterval":15,"quoteSourceName":"Delayed Quote","regularMarketOpen":{"raw":19.95,"fmt":"19.95"},"exchange":"TOR","regularMarketTime":{"raw":1611953942,"fmt":"3:59PM EST"},"fiftyTwoWeekRange":{"raw":"14.36 - 22.01","fmt":"14.36 - 22.01"},"regularMarketDayHigh":{"raw":19.95,"fmt":"19.95"},"shortName":"BMO US HIGH DIVIDEND COVERED CA","longName":"BMO US High Dividend Covered Call ETF","exchangeTimezoneName":"America/Toronto","regularMarketChange":{"raw":-0.3199997,"fmt":"-0.32"},"regularMarketPreviousClose":{"raw":20.02,"fmt":"20.02"},"fiftyTwoWeekHighChange":{"raw":-2.3099995,"fmt":"-2.31"},"exchangeTimezoneShortName":"EST","fiftyTwoWeekLowChange":{"raw":5.340001,"fmt":"5.34"},"exchangeDataDelayedBy":15,"regularMarketDayLow":{"raw":19.65,"fmt":"19.65"},"priceHint":2,"currency":"CAD","regularMarketPrice":{"raw":19.7,"fmt":"19.70"},"regularMarketVolume":{"raw":58550,"fmt":"58,550","longFmt":"58,550"},"isLoading":false,"triggerable":true,"gmtOffSetMilliseconds":-18000000,"firstTradeDateMilliseconds":1392388200000,"region":"US","marketState":"POSTPOST","quoteType":"ETF","invalid":false,"symbol":"ZWH.TO","language":"en-US","fiftyTwoWeekLowChangePercent":{"raw":0.37186638,"fmt":"37.19%"},"regularMarketDayRange":{"raw":"19.65 - 19.95","fmt":"19.65 - 19.95"},"messageBoardId":"finmb_253436770","fiftyTwoWeekHigh":{"raw":22.01,"fmt":"22.01"},"fiftyTwoWeekHighChangePercent":{"raw":-0.10495227,"fmt":"-10.50%"},"uuid":"a8bcb3d3-2a14-3815-9d69-4f6f49aa1c29","market":"ca_market","fiftyTwoWeekLow":{"raw":14.36,"fmt":"14.36"},"regularMarketChangePercent":{"raw":-1.5984001,"fmt":"-1.60%"},"fullExchangeName":"Toronto","tradeable":false},"ZWC.TO":{"sourceInterval":15,"quoteSourceName":"Delayed Quote","regularMarketOpen":{"raw":16.3,"fmt":"16.30"},"exchange":"TOR","regularMarketTime":{"raw":1611953970,"fmt":"3:59PM EST"},"fiftyTwoWeekRange":{"raw":"11.48 - 19.43","fmt":"11.48 - 19.43"},"regularMarketDayHigh":{"raw":16.33,"fmt":"16.33"},"shortName":"BMO CDN HIGH DIV COVERED CALL E","longName":"BMO CA High Dividend Covered Call ETF","exchangeTimezoneName":"America/Toronto","regularMarketChange":{"raw":-0.23999977,"fmt":"-0.24"},"regularMarketPreviousClose":{"raw":16.39,"fmt":"16.39"},"fiftyTwoWeekHighChange":{"raw":-3.2800007,"fmt":"-3.28"},"exchangeTimezoneShortName":"EST","fiftyTwoWeekLowChange":{"raw":4.67,"fmt":"4.67"},"exchangeDataDelayedBy":15,"regularMarketDayLow":{"raw":16.06,"fmt":"16.06"},"priceHint":2,"currency":"CAD","regularMarketPrice":{"raw":16.15,"fmt":"16.15"},"regularMarketVolume":{"raw":377094,"fmt":"377,094","longFmt":"377,094"},"isLoading":false,"triggerable":true,"gmtOffSetMilliseconds":-18000000,"firstTradeDateMilliseconds":1487082600000,"region":"US","marketState":"POSTPOST","quoteType":"ETF","invalid":false,"symbol":"ZWC.TO","language":"en-US","fiftyTwoWeekLowChangePercent":{"raw":0.40679446,"fmt":"40.68%"},"regularMarketDayRange":{"raw":"16.06 - 16.33","fmt":"16.06 - 16.33"},"messageBoardId":"finmb_414472744","fiftyTwoWeekHigh":{"raw":19.43,"fmt":"19.43"},"fiftyTwoWeekHighChangePercent":{"raw":-0.16881114,"fmt":"-16.88%"},"uuid":"9af002d6-34e0-3f56-b496-eb432a8bc694","market":"ca_market","fiftyTwoWeekLow":{"raw":11.48,"fmt":"11.48"},"regularMarketChangePercent":{"raw":-1.4643062,"fmt":"-1.46%"},"fullExchangeName":"Toronto","tradeable":false},"ZUT.TO":{"sourceInterval":15,"quoteSourceName":"Delayed Quote","regularMarketOpen":{"raw":25.9,"fmt":"25.90"},"exchange":"TOR","regularMarketTime":{"raw":1611953997,"fmt":"3:59PM EST"},"fiftyTwoWeekRange":{"raw":"14.53 - 27.14","fmt":"14.53 - 27.14"},"regularMarketDayHigh":{"raw":25.94,"fmt":"25.94"},"shortName":"BMO EQUAL WEIGHT UTILITIES INDE","longName":"BMO Equal Weight Utilities Index ETF","exchangeTimezoneName":"America/Toronto","regularMarketChange":{"raw":-0.19000053,"fmt":"-0.19"},"regularMarketPreviousClose":{"raw":25.95,"fmt":"25.95"},"fiftyTwoWeekHighChange":{"raw":-1.3799992,"fmt":"-1.38"},"exchangeTimezoneShortName":"EST","fiftyTwoWeekLowChange":{"raw":11.2300005,"fmt":"11.23"},"exchangeDataDelayedBy":15,"regularMarketDayLow":{"raw":25.43,"fmt":"25.43"},"priceHint":2,"currency":"CAD","regularMarketPrice":{"raw":25.76,"fmt":"25.76"},"regularMarketVolume":{"raw":142395,"fmt":"142,395","longFmt":"142,395"},"isLoading":false,"triggerable":true,"gmtOffSetMilliseconds":-18000000,"firstTradeDateMilliseconds":1264429800000,"region":"US","marketState":"POSTPOST","quoteType":"ETF","invalid":false,"symbol":"ZUT.TO","language":"en-US","fiftyTwoWeekLowChangePercent":{"raw":0.7728837,"fmt":"77.29%"},"regularMarketDayRange":{"raw":"25.43 - 25.94","fmt":"25.43 - 25.94"},"messageBoardId":"finmb_83524588","fiftyTwoWeekHigh":{"raw":27.14,"fmt":"27.14"},"fiftyTwoWeekHighChangePercent":{"raw":-0.050847426,"fmt":"-5.08%"},"uuid":"674b2651-4b39-3f74-8dcd-9f99c0b425fd","market":"ca_market","fiftyTwoWeekLow":{"raw":14.53,"fmt":"14.53"},"regularMarketChangePercent":{"raw":-0.7321793,"fmt":"-0.73%"},"fullExchangeName":"Toronto","tradeable":false}},"mktmData":{}}

请告诉我如何解决这个问题?

如果您提供了从API返回的JSON,那么我们可以查看它的结构并给出更好的答案,这会有所帮助

您确定要获取的子字符串是有效的JSON格式吗?否则,您将无法对它们执行
json.loads()


如果它们不是JSON格式或任何其他已知的结构化格式,您可能必须依靠使用自定义代码解析字符串。

当我尝试在本地计算机上运行代码时,我得到以下响应:

{'message': "API doesn't exists"}
但我认为这里的问题是,您的响应被格式化为一系列响应。因此,您必须通过
for
循环访问它们


如果这不能解决您的问题,请共享您得到的JSON响应的示例输出,以便更容易调试问题。

根据您提供的结构,这就是您所需要的:

print(source["fundProfile"]["feesExpensesInvestment"]["annualReportExpenseRatio"])
输出:

 {'raw': 0.0064999997, 'fmt': '0.65%'}
要仅获取
fmt
值,请使用以下命令:

print(source["fundProfile"]["feesExpensesInvestment"]["annualReportExpenseRatio"]["fmt"])
输出:
0.65%


如果结果有多个键值对,您可以使用循环对它们进行迭代,并获取所有值。

如果
的类型如您所指出的
str
,则不能使用
项[“annualReportExpenseRatio”][“fmt”]
。请提供
对象的结构,以便我们进一步提供帮助。我刚刚根据要求添加了源结构,API将要求我的帐户凭据正常工作。您好,我看不出这有什么原因不起作用。我建议您做的是,因为您得到的JSON不是数组,所以请避免使用for循环。只需使用
source[“fundProfile”][“feesepensesvestment”][“annualReportExpenseRatio”][“fmt”]
访问您所需的金额,我认为您不需要for循环,除非所需的记录位于JSON对象数组中。如果我不使用for循环,我会得到9次值,有没有办法将其限制为一次?没有,这就是我说的,如果响应是数组,那么必须使用for循环。您共享的示例没有将其显示为数组,这就是为什么我建议不要使用for loop现在就可以了:print(来源[“fundProfile”][“FeesePensesInvestment”][“annualReportExpenseRatio”][“fmt”]),但我得到了9次0.65%,我怎么能只得到一次呢?(回到最初的问题,如果我使用for循环,它将在不知道完整输入的情况下返回str keyerror,我不确定我能提供多少帮助。可能只是将元素添加到列表中,如果您不想全部使用,则仅使用第一个元素。我添加了从API发送的完整json。我不明白您为什么需要循环。我得到了所需的结果。)只需打印即可输出(源代码[“fundProfile”][“FeesePensesInvestment”][“annualReportExpenseRatio”])我爱你,伙计!!!现在可以用了!!!一整天都被困在里面了…因为我无意中缩进了你的代码,所以一直在循环。。。
print(source["fundProfile"]["feesExpensesInvestment"]["annualReportExpenseRatio"]["fmt"])