我沒有找到我的錯誤。 Eclipse告訴我:JSONArray類型中的方法getJSONObject(int)不適用於參數(字符串) 我想訪問:matchdata - > goals - > goal - > goal_getter_name eclipse錯誤在這一行上:`JSONObject goal = openbuli.getJSONObject(「goal」); 我認爲變量openbuli ist錯了?! 我該如何解決它?android:JSONArray類型的getJSONObject(int)方法不適用於參數(字符串)
這裏北京時間我的代碼:
String result = "";
SONObject jArray = null;
jArray = new JSONObject(result);
JSONObject json = (JSONObject) jArray;
JSONArray openbuli = json.getJSONArray("matchdata");
JSONObject goal = openbuli.getJSONObject("goal");
JSONArray goals = goal.getJSONArray("goals");
// loop array
for(int i=0;i<goals.length();i++) {
JSONObject e = goals.getJSONObject(i);
Log.e("Name", e.getString("goal_getter_name").toString());
}
這裏JSON數據:
$json (
| matchdata => Array (9)
| (
| | ['0'] (
| | | league_saison = "2013"
| | | match_results (
| | | | match_result => Array (2)
| | | | (
| | | | | ['0'] (
| | | | | | result_name = "Endergebnis"
| | | | | | result_order_id = "1"
| | | | | | points_team1 = "3"
| | | | | | result_type_name = "nach 90 Minuten"
| | | | | | points_team2 = "1"
| | | | | | result_type_id = "2"
| | | | | )
| | | | )
| | | )
| | | goals (
| | | | goal => Array (4)
| | | | (
| | | | | ['0'] (
| | | | | | goal_match_minute = "16"
| | | | | | goal_getter_id = "5112"
| | | | | | goal_id = "21118"
| | | | | | goal_getter_name = "Mario Mandzukic"
| | | | | | goal_mach_id = "23711"
| | | | | | goal_penalty = FALSE
| | | | | | goal_score_team1 = "2"
| | | | | | goal_own_goal = FALSE
| | | | | | goal_score_team2 = "0"
| | | | | | goal_overtime = FALSE
| | | | | )
| | | | )
| | | )
| | )
你不能做到這一點'openbuli.getJSONObject(「目標」)中;'你必須給該方法是數組的索引。例如:'openbuli.getJSONObject(1);' – ZeusNet