因此,我正在研究一個Android應用程序,它將從遠程服務器(運行PHP)讀取JSON響應。一切正常,直到我嘗試從較大的JSON字符串讀取數組數據項。下面是Eclipse在嘗試將對象分析到數組中時拋出的錯誤。有人有主意嗎? JSONLint驗證這個數組是有效的JSON,所以我不確定這裏的問題是什麼。
注意 - 它是否讓我把這個元素解析爲一個JSONObject,但我不清楚如何從它那裏得到它到像我需要的關聯數組。我確實看到JSONObject有一個方法ToJSONArray,但它需要一個數組鍵,但這些變量是可變的,我不知道它們是什麼,直到我解析該死的數組!任何援助表示讚賞。從字符串中解析JSON數組問題
public JSONArray parseJson(String jsonString) {
JSONArray data = new JSONArray();
try {
JSONObject json = new JSONObject(jsonString);
success = json.getBoolean("success");
object = json.getString("object");
message = json.getString("message");
data = json.getJSONArray("data")
} catch (JSONException e) {
Log.e("ServerResponse::parseJson", e.getMessage());
e.printStackTrace();
success = false;
message = "Error parsing return JSON data. " + e.getCause();
}
return data;
}
這裏的JSON數組我試圖解析:
{
"1": {
"country_of_origin": "US",
"genre": "Action",
"date_finished": "2005-06-17",
"dtimemod": "2009-12-03 14:29:59",
"netflix_rating": "82",
"table": "movies",
"rotten_rating": "82",
"format": "Live Action",
"date_begun": "2005-06-17",
"id": "3055",
"dtimecre": "2009-12-03 14:29:59",
"name": "Batman Begins",
"user_id": "mcenci",
"year": "2005",
"actors": "Christian Bale, Michael Caine, Liam Neeson, Morgan Freeman, Gary Oldman, Ken Watanabe, Katie Holmes, Cillian Murphy, Tom Wilkinson, Rutger Hauer, Sara Stewart, Richard Brake, Gus Lewis",
"rating": "100",
"extra_info": "Runtime: 140 min\n",
"public": "0",
"notes": "",
"key": "103",
"genre2": "Sci-Fi"
}
}
「下面是錯誤的Eclipse拋出」< - 你忘了實際包括。 –