如何在Android中解析此JSON?

时间:2010-08-26 20:38:36

标签: android json parsing

我希望能够提取不同的字段,例如Name和Symbol等。如何在Android中创建我的JSONObject来执行此操作?

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

此代码可以提取引用,但我不确定如何获取单个字段:

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

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

2 个答案:

答案 0 :(得分:0)

JSONObject对象提供了获取所有类型值的方法。例如,你说:

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

答案 1 :(得分:0)

我喜欢这样

    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);
    }