4
    {
  "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'm getting and I'm trying to get MarketCapitalization out of this using:

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

by doing following operation:

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

but I'm getting 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 is working completely fine. The problem is occurring only when it is null

5 Answers 5

8

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

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

See documentation

Sign up to request clarification or add additional context in comments.

Comments

5

Try doing some proactive check for null before use, E.g. :

jsonObj.isNull("MarketCapitalization"){

//do something

}

1 Comment

thanks a lot for your answer. Helped me out in saving details with optional values...One upvote from me!!
1

You could try getObject() instead of getString(), then check for null. But that's obviously not a strong point of the library you are using...

1 Comment

No, It's not working now i'm getting the following error org.json.JSONException: JSONObject["MarketCapitalization"] is not a JSONObject.
0

First check wheather the value of key "MarketCapitalization" is null or not if not null the do your work

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

Comments

0

JSONObject receives an array for each data field, takes this data and transfers it to another variable, such as an array or string using the field name as a reference, such as 'value', 'name' and etc... If null, transfer a value in white to avoid mistakes. I do like this...

String value; 

if(jsonObject.get("name").toString().equals("null")) {
  value = ""; 
}else {
  value = jsonObject.getString("name"); 
}

1 Comment

Welcome to Stack Overflow. Code-only answers are discouraged on Stack Overflow because they don't explain how it solves the problem. Please edit your answer to explain what this code does and how it answers the question, so that it is useful to the OP as well as other users with similar issues.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.