JSONObject is not a string error

    {
  "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
      }
    }
  }
}

      

This is the Json I am getting and I am trying to get MarketCapitalization from this using:

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

      

by performing the following operation:

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

      

but i am getting the following error

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)

      

Note. If in case MarketCapitalization has some value, then the above works completely fine. The problem only occurs when it is null

+3


source to share


4 answers


Use optString () to get the value if it exists otherwise, or the default value you defined:

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

      



See documentation

+4


source


Try pre-checking on null

before use, e.g .:



jsonObj.isNull("MarketCapitalization"){

//do something

}

      

+3


source


You can try getObject()

instead getString()

and then check for null. But this is clearly not the strong point of the library you are using ...

+1


source


First check that the value of the "MarketCapitalization" key is null or not. if not null do your job

    JSONObject quoteJson = obj.getJSONObject("query").getJSONObject("results")
            .getJSONObject("quote");
    if(quoteJson.get("MarketCapitalization")!=null)
    {
        //do your work
    }

      

0


source







All Articles