gpt4 book ai didi

java - JSONObject 不是字符串错误

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:54:40 25 4
gpt4 key购买 nike

    {
"query": {
"count": 1,
"created": "2015-07-28T05:19:01Z",
"lang": "en-US",
"results": {
"quote": {
"symbol": "GITANJALI.NS",
"Ask": null,
"AverageDailyVolume": null,
"Bid": null,
"AskRealtime": null,
"BidRealtime": null,
"BookValue": null,
"Change_PercentChange": null,
"Change": null,
"Commission": null,
"Currency": null,
"ChangeRealtime": null,
"AfterHoursChangeRealtime": null,
"DividendShare": null,
"LastTradeDate": null,
"TradeDate": null,
"EarningsShare": "9.73",
"ErrorIndicationreturnedforsymbolchangedinvalid": null,
"EPSEstimateCurrentYear": null,
"EPSEstimateNextYear": null,
"EPSEstimateNextQuarter": null,
"DaysLow": null,
"DaysHigh": null,
"YearLow": null,
"YearHigh": null,
"HoldingsGainPercent": null,
"AnnualizedGain": null,
"HoldingsGain": null,
"HoldingsGainPercentRealtime": null,
"HoldingsGainRealtime": null,
"MoreInfo": null,
"OrderBookRealtime": null,
"MarketCapitalization": null,
"MarketCapRealtime": null,
"EBITDA": null,
"ChangeFromYearLow": null,
"PercentChangeFromYearLow": null,
"LastTradeRealtimeWithTime": null,
"ChangePercentRealtime": null,
"ChangeFromYearHigh": null,
"PercebtChangeFromYearHigh": null,
"LastTradeWithTime": null,
"LastTradePriceOnly": null,
"HighLimit": null,
"LowLimit": null,
"DaysRange": null,
"DaysRangeRealtime": null,
"FiftydayMovingAverage": null,
"TwoHundreddayMovingAverage": null,
"ChangeFromTwoHundreddayMovingAverage": null,
"PercentChangeFromTwoHundreddayMovingAverage": null,
"ChangeFromFiftydayMovingAverage": null,
"PercentChangeFromFiftydayMovingAverage": null,
"Name": null,
"Notes": null,
"Open": null,
"PreviousClose": null,
"PricePaid": null,
"ChangeinPercent": null,
"PriceSales": null,
"PriceBook": null,
"ExDividendDate": null,
"PERatio": null,
"DividendPayDate": null,
"PERatioRealtime": null,
"PEGRatio": null,
"PriceEPSEstimateCurrentYear": null,
"PriceEPSEstimateNextYear": null,
"Symbol": "GITANJALI.NS",
"SharesOwned": null,
"ShortRatio": null,
"LastTradeTime": null,
"TickerTrend": null,
"OneyrTargetPrice": null,
"Volume": null,
"HoldingsValue": null,
"HoldingsValueRealtime": null,
"YearRange": null,
"DaysValueChange": null,
"DaysValueChangeRealtime": null,
"StockExchange": null,
"DividendYield": null,
"PercentChange": null
}
}
}
}

这是我得到的 Json,我正在尝试使用以下方法从中获取 MarketCapitalization:

import org.json.JSONArray;
import org.json.JSONObject;

通过执行以下操作:

String marketCap = obj.getJSONObject("query").getJSONObject("results")
.getJSONObject("quote").getString("MarketCapitalization");

但是我遇到了以下错误

org.json.JSONException: JSONObject["MarketCapitalization"] not a string.
at org.json.JSONObject.getString(JSONObject.java:658)
at com.mobileforce.YahooClient.prepareReadCompany(YahooClient.java:136)
at com.mobileforce.YahooClient.read(YahooClient.java:82)
at com.mobileforce.YahooClient.handleApi(YahooClient.java:42)
at com.mobileforce.YahooServlet.doPost(YahooServlet.java:44)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:957)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:620)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

注意:如果 MarketCapitalization 有一定的值(value),那么上面的方法完全可以正常工作。只有当它为 null 时才会出现问题

最佳答案

如果值存在则使用 optString() 获取值,否则为 null 或您定义的默认值:

.optString("MarketCapitalization", "defaultValue");

参见 documentation

关于java - JSONObject 不是字符串错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31668023/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com