Ben Smith
Ben Smith

Reputation: 51

Accessing a JSON object inside another JSON object

Here is part of the json file I am using (from YQL).

{
"query":{
  "count":1,
  "created":"2014-08-03T11:11:01Z",
  "lang":"en-US",
  "results":{
     "quote":{
        "symbol":"GOOGL",
        "Ask":"575.00",
      }
   }
 }
}

Here is the code I tried to use to parse:

           // get json string from url
            JSONObject json = jParser.getJSONFromUrl(JsonStringUrl);

            // get the array of users
            JSONObject query = json.getJSONObject("query");
            JSONObject results = query.getJSONObject("results");
            JSONObject quote  = results.getJSONObject("quote");

            String ask = quote.getString("ask");
            Log.e(TAG, "Ask: " + ask);

This doesn't work (as I assumed it wouldn't) but I am not really sure how I would go about changing it. Thanks for taking the time to read/answer this :)

Upvotes: 0

Views: 83

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1075159

That code looks fine to me other than that you've requested the value for the key "ask", and the key in the JSON is "Ask" (capital A). (Capitalization matters in JSON.)

Upvotes: 3

Related Questions