我有以下JSON:我該如何解析這個JSON而不會得到異常?
[
{
"outcome": "Success",
"message": "",
"identity": "",
"delay": "0",
"symbol": "AAPL",
"companyname": "Apple Inc.",
"date": "Jun 08",
"time": " 4:52 PM EDT",
"open": "330.88",
"close": "332",
"previousclose": "332.04",
"high": "334.8",
"low": "330.51",
"last": "332",
"change": "-0.04",
"percentchange": "-0.012",
"volume": "1239421",
"created_at": "1307581193"
},
{
"outcome": "Success",
"message": "",
"identity": "",
"delay": "0",
"symbol": "GOOG",
"companyname": "Google Inc.",
"date": "Jun 08",
"time": " 3:59 PM EDT",
"open": "516.76",
"close": "519.28",
"previousclose": "519.03",
"high": "521.14",
"low": "515.79",
"last": "519.28",
"change": "0.25",
"percentchange": "0.048",
"volume": "229886",
"created_at": "1307576900"
}
]
這裏是我的Java:
JSONArray jquotes = new JSONArray(json.toString());
if (jquotes.length() > 0) {
for (int i = 0; i < jquotes.length(); i++) {
JSONObject quote = jquotes.getJSONObject(i);
Quote myQuote = new Quote();
myQuote.setName(quote.getString("companyname"));
myQuote.setSymbol(quote.getString("symbol"));
myQuote.setLastTradePriceOnly(quote.getString("last"));
myQuote.setChange(quote.getString("change"));
myQuote.setPercentChange(quote.getString("percentchange"));
quotesAdapter.add(myQuote);
}
}
我得到異常:
value of type org.json.JSONArray cannot be converted to JSONObject
at org.json.JSON.typeMismatch(JSON.java:107)
順便說一句,你完全可能要考慮GSON:http://code.google.com/p/google-gson/ – 2011-06-09 02:55:12