views:

112

answers:

1

Here is the JSON result:

{
  "query": {
    "count": "4",
    "created": "2010-08-31T05:53:39Z",
    "lang": "en-US",
    "results": {
      "quote": [
        {
          "symbol": "AAPL",
          "Ask": "244.17",
          "AverageDailyVolume": "23152200",
          "Bid": "244.12",
          "AskRealtime": "244.17",
          "BidRealtime": "244.12",
          "BookValue": "47.194",
          "Change_PercentChange": "+1.67 - +0.69%",
          "Change": "+1.67",
          "Commission": null,
          "ChangeRealtime": "+1.67",
          "AfterHoursChangeRealtime": "N/A - N/A",
          "DividendShare": "0.00",
          "LastTradeDate": "8/31/2010",
          "TradeDate": null,
          "EarningsShare": "13.285",
          "ErrorIndicationreturnedforsymbolchangedinvalid": "N/A",
          "EPSEstimateCurrentYear": "14.43",
          "EPSEstimateNextYear": "17.48",
          "EPSEstimateNextQuarter": "4.89",
          "DaysLow": "240.35",
          "DaysHigh": "244.56",
          "YearLow": "164.11",
          "YearHigh": "279.01",
          "HoldingsGainPercent": "- - -",
          "AnnualizedGain": "-",
          "HoldingsGain": null,
          "HoldingsGainPercentRealtime": "N/A - N/A",
          "HoldingsGainRealtime": null,
          "MoreInfo": "cnsprmiIed",
          "OrderBookRealtime": "N/A",
          "MarketCapitalization": "223.1B",
          "MarketCapRealtime": null,
          "EBITDA": "17.498B",
          "ChangeFromYearLow": "+80.06",
          "PercentChangeFromYearLow": "+48.78%",
          "LastTradeRealtimeWithTime": "N/A - <b>244.17</b>",
          "ChangePercentRealtime": "N/A - +0.69%",
          "ChangeFromYearHigh": "-34.84",
          "PercebtChangeFromYearHigh": "-12.49%",
          "LastTradeWithTime": "1:38pm - <b>244.17</b>",
          "LastTradePriceOnly": "244.17",
          "HighLimit": null,
          "LowLimit": null,
          "DaysRange": "240.35 - 244.56",
          "DaysRangeRealtime": "N/A - N/A",
          "FiftydayMovingAverage": "253.32",
          "TwoHundreddayMovingAverage": "244.902",
          "ChangeFromTwoHundreddayMovingAverage": "-0.732",
          "PercentChangeFromTwoHundreddayMovingAverage": "-0.30%",
          "ChangeFromFiftydayMovingAverage": "-9.15",
          "PercentChangeFromFiftydayMovingAverage": "-3.61%",
          "Name": "Apple Inc.",
          "Notes": "-",
          "Open": "241.79",
          "PreviousClose": "242.50",
          "PricePaid": null,
          "ChangeinPercent": "+0.69%",
          "PriceSales": "3.88",
          "PriceBook": "5.14",
          "ExDividendDate": "21-Nov-95",
          "PERatio": "18.25",
          "DividendPayDate": "N/A",
          "PERatioRealtime": null,
          "PEGRatio": "0.88",
          "PriceEPSEstimateCurrentYear": "16.81",
          "PriceEPSEstimateNextYear": "13.87",
          "Symbol": "AAPL",
          "SharesOwned": null,
          "ShortRatio": "0.40",
          "LastTradeTime": "1:38pm",
          "TickerTrend": "&nbsp;--+-++&nbsp;",
          "OneyrTargetPrice": "332.00",
          "Volume": "7916913",
          "HoldingsValue": null,
          "HoldingsValueRealtime": null,
          "YearRange": "164.11 - 279.01",
          "DaysValueChange": "- - +0.69%",
          "DaysValueChangeRealtime": "N/A - N/A",
          "StockExchange": "NasdaqNM",
          "DividendYield": null,
          "PercentChange": "+0.69%"
        },
        {
          "symbol": "MSFT",
          "Ask": "23.60",
          "AverageDailyVolume": "67088400",
          "Bid": "23.59",
          "AskRealtime": "23.60",
          "BidRealtime": "23.59",
          "BookValue": "5.327",
          "Change_PercentChange": "-0.045 - -0.19%",
          "Change": "-0.045",
          "Commission": null,
          "ChangeRealtime": "-0.045",
          "AfterHoursChangeRealtime": "N/A - N/A",
          "DividendShare": "0.52",
          "LastTradeDate": "8/31/2010",
          "TradeDate": null,
          "EarningsShare": "2.101",
          "ErrorIndicationreturnedforsymbolchangedinvalid": "N/A",
          "EPSEstimateCurrentYear": "2.36",
          "EPSEstimateNextYear": "2.64",
          "EPSEstimateNextQuarter": "0.69",
          "DaysLow": "23.52",
          "DaysHigh": "23.73",
          "YearLow": "22.73",
          "YearHigh": "31.58",
          "HoldingsGainPercent": "- - -",
          "AnnualizedGain": "-",
          "HoldingsGain": null,
          "HoldingsGainPercentRealtime": "N/A - N/A",
          "HoldingsGainRealtime": null,
          "MoreInfo": "cnsprmiIed",
          "OrderBookRealtime": "N/A",
          "MarketCapitalization": "204.2B",
          "MarketCapRealtime": null,
          "EBITDA": "27.197B",
          "ChangeFromYearLow": "+0.865",
          "PercentChangeFromYearLow": "+3.81%",
          "LastTradeRealtimeWithTime": "N/A - <b>23.595</b>",
          "ChangePercentRealtime": "N/A - -0.19%",
          "ChangeFromYearHigh": "-7.985",
          "PercebtChangeFromYearHigh": "-25.28%",
          "LastTradeWithTime": "1:38pm - <b>23.595</b>",
          "LastTradePriceOnly": "23.595",
          "HighLimit": null,
          "LowLimit": null,
          "DaysRange": "23.52 - 23.73",
          "DaysRangeRealtime": "N/A - N/A",
          "FiftydayMovingAverage": "25.0947",
          "TwoHundreddayMovingAverage": "27.3083",
          "ChangeFromTwoHundreddayMovingAverage": "-3.7133",
          "PercentChangeFromTwoHundreddayMovingAverage": "-13.60%",
          "ChangeFromFiftydayMovingAverage": "-1.4997",
          "PercentChangeFromFiftydayMovingAverage": "-5.98%",
          "Name": "Microsoft Corpora",
          "Notes": "-",
          "Open": "23.58",
          "PreviousClose": "23.64",
          "PricePaid": null,
          "ChangeinPercent": "-0.19%",
          "PriceSales": "3.27",
          "PriceBook": "4.44",
          "ExDividendDate": "Aug 17",
          "PERatio": "11.25",
          "DividendPayDate": "Sep  9",
          "PERatioRealtime": null,
          "PEGRatio": "0.97",
          "PriceEPSEstimateCurrentYear": "10.02",
          "PriceEPSEstimateNextYear": "8.95",
          "Symbol": "MSFT",
          "SharesOwned": null,
          "ShortRatio": "1.20",
          "LastTradeTime": "1:38pm",
          "TickerTrend": "&nbsp;-=+-+-&nbsp;",
          "OneyrTargetPrice": "33.03",
          "Volume": "25999320",
          "HoldingsValue": null,
          "HoldingsValueRealtime": null,
          "YearRange": "22.73 - 31.58",
          "DaysValueChange": "- - -0.19%",
          "DaysValueChangeRealtime": "N/A - N/A",
          "StockExchange": "NasdaqNM",
          "DividendYield": "2.20",
          "PercentChange": "-0.19%"
        },
        {
          "symbol": "GOOG",
          "Ask": "451.95",
          "AverageDailyVolume": "2813780",
          "Bid": "451.68",
          "AskRealtime": "451.95",
          "BidRealtime": "451.68",
          "BookValue": "127.468",
          "Change_PercentChange": "-0.89 - -0.20%",
          "Change": "-0.89",
          "Commission": null,
          "ChangeRealtime": "-0.89",
          "AfterHoursChangeRealtime": "N/A - N/A",
          "DividendShare": "0.00",
          "LastTradeDate": "8/31/2010",
          "TradeDate": null,
          "EarningsShare": "23.025",
          "ErrorIndicationreturnedforsymbolchangedinvalid": "N/A",
          "EPSEstimateCurrentYear": "27.27",
          "EPSEstimateNextYear": "31.26",
          "EPSEstimateNextQuarter": "7.50",
          "DaysLow": "448.00",
          "DaysHigh": "454.87",
          "YearLow": "433.63",
          "YearHigh": "629.51",
          "HoldingsGainPercent": "- - -",
          "AnnualizedGain": "-",
          "HoldingsGain": null,
          "HoldingsGainPercentRealtime": "N/A - N/A",
          "HoldingsGainRealtime": null,
          "MoreInfo": "cnprmiIed",
          "OrderBookRealtime": "N/A",
          "MarketCapitalization": "144.0B",
          "MarketCapRealtime": null,
          "EBITDA": "10.825B",
          "ChangeFromYearLow": "+18.17",
          "PercentChangeFromYearLow": "+4.19%",
          "LastTradeRealtimeWithTime": "N/A - <b>451.80</b>",
          "ChangePercentRealtime": "N/A - -0.20%",
          "ChangeFromYearHigh": "-177.71",
          "PercebtChangeFromYearHigh": "-28.23%",
          "LastTradeWithTime": "1:38pm - <b>451.80</b>",
          "LastTradePriceOnly": "451.80",
          "HighLimit": null,
          "LowLimit": null,
          "DaysRange": "448.00 - 454.87",
          "DaysRangeRealtime": "N/A - N/A",
          "FiftydayMovingAverage": "481.689",
          "TwoHundreddayMovingAverage": "511.725",
          "ChangeFromTwoHundreddayMovingAverage": "-59.925",
          "PercentChangeFromTwoHundreddayMovingAverage": "-11.71%",
          "ChangeFromFiftydayMovingAverage": "-29.889",
          "PercentChangeFromFiftydayMovingAverage": "-6.21%",
          "Name": "Google Inc.",
          "Notes": "-",
          "Open": "450.11",
          "PreviousClose": "452.69",
          "PricePaid": null,
          "ChangeinPercent": "-0.20%",
          "PriceSales": "5.50",
          "PriceBook": "3.55",
          "ExDividendDate": "N/A",
          "PERatio": "19.66",
          "DividendPayDate": "N/A",
          "PERatioRealtime": null,
          "PEGRatio": "1.03",
          "PriceEPSEstimateCurrentYear": "16.60",
          "PriceEPSEstimateNextYear": "14.48",
          "Symbol": "GOOG",
          "SharesOwned": null,
          "ShortRatio": "1.40",
          "LastTradeTime": "1:38pm",
          "TickerTrend": "&nbsp;=+=-=-&nbsp;",
          "OneyrTargetPrice": "619.52",
          "Volume": "874781",
          "HoldingsValue": null,
          "HoldingsValueRealtime": null,
          "YearRange": "433.63 - 629.51",
          "DaysValueChange": "- - -0.20%",
          "DaysValueChangeRealtime": "N/A - N/A",
          "StockExchange": "NasdaqNM",
          "DividendYield": null,
          "PercentChange": "-0.20%"
        },
        {
          "symbol": "YHOO",
          "Ask": "13.06",
          "AverageDailyVolume": "18903300",
          "Bid": "13.05",
          "AskRealtime": "13.06",
          "BidRealtime": "13.05",
          "BookValue": "8.912",
          "Change_PercentChange": "-0.12 - -0.91%",
          "Change": "-0.12",
          "Commission": null,
          "ChangeRealtime": "-0.12",
          "AfterHoursChangeRealtime": "N/A - N/A",
          "DividendShare": "0.00",
          "LastTradeDate": "8/31/2010",
          "TradeDate": null,
          "EarningsShare": "0.61",
          "ErrorIndicationreturnedforsymbolchangedinvalid": "N/A",
          "EPSEstimateCurrentYear": "0.69",
          "EPSEstimateNextYear": "0.74",
          "EPSEstimateNextQuarter": "0.19",
          "DaysLow": "12.97",
          "DaysHigh": "13.135",
          "YearLow": "13.03",
          "YearHigh": "19.12",
          "HoldingsGainPercent": "- - -",
          "AnnualizedGain": "-",
          "HoldingsGain": null,
          "HoldingsGainPercentRealtime": "N/A - N/A",
          "HoldingsGainRealtime": null,
          "MoreInfo": "cnsprmiIed",
          "OrderBookRealtime": "N/A",
          "MarketCapitalization": "17.609B",
          "MarketCapRealtime": null,
          "EBITDA": "1.328B",
          "ChangeFromYearLow": "+0.03",
          "PercentChangeFromYearLow": "+0.23%",
          "LastTradeRealtimeWithTime": "N/A - <b>13.06</b>",
          "ChangePercentRealtime": "N/A - -0.91%",
          "ChangeFromYearHigh": "-6.06",
          "PercebtChangeFromYearHigh": "-31.69%",
          "LastTradeWithTime": "1:38pm - <b>13.06</b>",
          "LastTradePriceOnly": "13.06",
          "HighLimit": null,
          "LowLimit": null,
          "DaysRange": "12.97 - 13.135",
          "DaysRangeRealtime": "N/A - N/A",
          "FiftydayMovingAverage": "14.1167",
          "TwoHundreddayMovingAverage": "15.504",
          "ChangeFromTwoHundreddayMovingAverage": "-2.444",
          "PercentChangeFromTwoHundreddayMovingAverage": "-15.76%",
          "ChangeFromFiftydayMovingAverage": "-1.0567",
          "PercentChangeFromFiftydayMovingAverage": "-7.49%",
          "Name": "Yahoo! Inc.",
          "Notes": "-",
          "Open": "13.09",
          "PreviousClose": "13.18",
          "PricePaid": null,
          "ChangeinPercent": "-0.91%",
          "PriceSales": "2.73",
          "PriceBook": "1.48",
          "ExDividendDate": "12-May-04",
          "PERatio": "21.61",
          "DividendPayDate": "N/A",
          "PERatioRealtime": null,
          "PEGRatio": "1.73",
          "PriceEPSEstimateCurrentYear": "19.10",
          "PriceEPSEstimateNextYear": "17.81",
          "Symbol": "YHOO",
          "SharesOwned": null,
          "ShortRatio": "1.40",
          "LastTradeTime": "1:38pm",
          "TickerTrend": "&nbsp;===-=+&nbsp;",
          "OneyrTargetPrice": "18.16",
          "Volume": "7561435",
          "HoldingsValue": null,
          "HoldingsValueRealtime": null,
          "YearRange": "13.03 - 19.12",
          "DaysValueChange": "- - -0.91%",
          "DaysValueChangeRealtime": "N/A - N/A",
          "StockExchange": "NasdaqNM",
          "DividendYield": null,
          "PercentChange": "-0.91%"
        }
      ]
    }
  }
}

UPDATE: And here is my code (assume buildQuery and RestClient are valid):

private void getQuotesFromYQL(){
    JSONObject json = RestClient
    .connect(buildQuery());
    try {

        JSONObject query = json.getJSONObject("query");
        JSONArray quotes = query.getJSONArray("results");
        for (int i = 0; i < quotes.length(); i++) {

            JSONObject quote = quotes.getJSONObject(i)
            .getJSONObject("quote");

            // Do something with the user
            Quote myQuote = new Quote();
            myQuote.setName(quote.getString("Name"));
            myQuote.setSymbol(quote.getString("Symbol"));
            myQuote.setLastTradePriceOnly(quote.getString("LastTradePriceOnly"));

            quotesAdapter.add(myQuote);
        }

    } catch (JSONException e) {
        System.out.println(e);
    }
}   

I get the following Exception on JSONArray quotes = query.getJSONArray("results");:

org.json.JSONException: Value {"quote": ... 

The value of "quote" is printed in the debugger.

How can I parse this correctly?

+2  A: 

You need an extra JSONObject access to get to the "query" object:

JSONObject query = json.getJSONObject("query");
JSONObject results = query.getJSONObject("results");
JSONArray quotes = results.getJSONArray("quotes");
Frank
Thanks! Looks like I still get an Exception though. Now it's printing the value of the first Quote in the JSON in the debugger: org.json.JSONException: Value {"quote": ...
Sheehan Alam
The exception occurs on JSONArray quotes = query.getJSONArray("results");
Sheehan Alam
Ah just noticed the extra layer after results, I edited the original post check to see if that works
Frank