{
"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有一些价值,那么上述工作完全正常。只有当它为空时才会出现问题
答案 0 :(得分:5)
使用optString()获取值,如果它存在,否则为null或您定义的默认值:
.optString("MarketCapitalization", "defaultValue");
答案 1 :(得分:3)
在使用前尝试对viewDidLoad
进行一些主动检查,例如:
null
答案 2 :(得分:1)
您可以尝试result
而不是getObject()
,然后检查是否为空。但这显然不是你正在使用的图书馆的一个优势......
答案 3 :(得分:0)
首先检查关键字“MarketCapitalization”的值是否为空 如果不是null你做的工作
JSONObject quoteJson = obj.getJSONObject("query").getJSONObject("results")
.getJSONObject("quote");
if(quoteJson.get("MarketCapitalization")!=null)
{
//do your work
}
答案 4 :(得分:0)
我喜欢这样...
String value;
if(jsonObject.get("name").toString().equals("null")) {
value = "";
}else {
value = jsonObject.getString("name");
}