views:

68

answers:

2

I would like to be able to pull out different fields like Name and Symbol etc. How can I create my JSONObject in Android to do this?

 {
      "query": {
        "count": "1",
        "created": "2010-08-26T07:09:40Z",
        "lang": "en-US",
        "results": {
          "quote": {
            "symbol": "AAPL",
            "Ask": "241.07",
            "AverageDailyVolume": "23885200",
            "Bid": "241.04",
            "AskRealtime": "241.07",
            "BidRealtime": "241.04",
            "BookValue": "47.194",
            "Change_PercentChange": "-1.88 - -0.77%",
            "Change": "-1.88",
            "Commission": null,
            "ChangeRealtime": "-1.88",
            "AfterHoursChangeRealtime": "N/A - N/A",
            "DividendShare": "0.00",
            "LastTradeDate": "8/26/2010",
            "TradeDate": null,
            "EarningsShare": "13.285",
            "ErrorIndicationreturnedforsymbolchangedinvalid": "N/A",
            "EPSEstimateCurrentYear": "14.43",
            "EPSEstimateNextYear": "17.48",
            "EPSEstimateNextQuarter": "4.89",
            "DaysLow": "240.91",
            "DaysHigh": "245.74",
            "YearLow": "164.11",
            "YearHigh": "279.01",
            "HoldingsGainPercent": "- - -",
            "AnnualizedGain": "-",
            "HoldingsGain": null,
            "HoldingsGainPercentRealtime": "N/A - N/A",
            "HoldingsGainRealtime": null,
            "MoreInfo": "cnsprmiIed",
            "OrderBookRealtime": "N/A",
            "MarketCapitalization": "220.2B",
            "MarketCapRealtime": null,
            "EBITDA": "17.498B",
            "ChangeFromYearLow": "+76.90",
            "PercentChangeFromYearLow": "+46.86%",
            "LastTradeRealtimeWithTime": "N/A - <b>241.01</b>",
            "ChangePercentRealtime": "N/A - -0.77%",
            "ChangeFromYearHigh": "-38.00",
            "PercebtChangeFromYearHigh": "-13.62%",
            "LastTradeWithTime": "2:54pm - <b>241.01</b>",
            "LastTradePriceOnly": "241.01",
            "HighLimit": null,
            "LowLimit": null,
            "DaysRange": "240.91 - 245.74",
            "DaysRangeRealtime": "N/A - N/A",
            "FiftydayMovingAverage": "254.598",
            "TwoHundreddayMovingAverage": "243.902",
            "ChangeFromTwoHundreddayMovingAverage": "-2.892",
            "PercentChangeFromTwoHundreddayMovingAverage": "-1.19%",
            "ChangeFromFiftydayMovingAverage": "-13.588",
            "PercentChangeFromFiftydayMovingAverage": "-5.34%",
            "Name": "Apple Inc.",
            "Notes": "-",
            "Open": "245.48",
            "PreviousClose": "242.89",
            "PricePaid": null,
            "ChangeinPercent": "-0.77%",
            "PriceSales": "3.89",
            "PriceBook": "5.15",
            "ExDividendDate": "21-Nov-95",
            "PERatio": "18.28",
            "DividendPayDate": "N/A",
            "PERatioRealtime": null,
            "PEGRatio": "0.89",
            "PriceEPSEstimateCurrentYear": "16.83",
            "PriceEPSEstimateNextYear": "13.90",
            "Symbol": "AAPL",
            "SharesOwned": null,
            "ShortRatio": "0.40",
            "LastTradeTime": "2:54pm",
            "TickerTrend": "&nbsp;==+=+-&nbsp;",
            "OneyrTargetPrice": "332.00",
            "Volume": "11819994",
            "HoldingsValue": null,
            "HoldingsValueRealtime": null,
            "YearRange": "164.11 - 279.01",
            "DaysValueChange": "- - -0.77%",
            "DaysValueChangeRealtime": "N/A - N/A",
            "StockExchange": "NasdaqNM",
            "DividendYield": null,
            "PercentChange": "-0.77%"
          }
        }
      }
    }

This code can pull out quote, but I'm not sure how to get the individual fields:

JSONObject query = (json.getJSONObject("query").
                                    getJSONObject("results").
                                    getJSONObject("quote"));

            System.out.println(query.toString());
A: 

The JSONObject object provides methods for getting all the types of values. For instance you say:

System.out.println(query.getString("symbol")); // prints AAPL System.out.println(query.getDouble("EarningsShare"); // prints 13.285

Savvas Dalkitsis
A: 

I like this way

    quoteHashMap = new HashMap<String,String>();
    JSONObject actualObject = query.getJSONObject("query");
    JSONObject quote = query.getJSONObject("quote");
    JSONArray names = quote.names();
    JSONArray quoteArray = quote.toJSONArray(names);
    for(int i = 0; i < quoteArray.length(); i++){
        String value = quoteArray.getJSONObject(i).getString("value");
        String name = quoateArray.getJSONObject(i).getString("name");
        quoteHashMap.put(name, value);
    }
schwiz