0

我没有发现我的错误。Eclipse 告诉我:JSONArray 类型中的方法 getJSONObject(int) 不适用于我要访问的参数(字符串):matchdata -> 目标 -> 目标 -> 目标getter_name Eclipse 错误在这一行:`JSONObject 目标 = openbuli.getJSONObject("目标"); 我认为变量 openbuli 是错误的?!我必须如何修复它?

这是我的代码:

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
|    |    |    |    |    )
|    |    |    |    )
|    |    |    )
|    |    )
4

1 回答 1

1

您需要更改openbuli.getJSONObject("goal");为 openbuli.getJSONObject(0);
并添加 getJSONObject("goals") 以从数组的第一个位置获取对象目标

String result = "";
SONObject jArray = null;

jArray = new JSONObject(result);

                JSONObject json = (JSONObject) jArray;                 
                JSONArray openbuli = json.getJSONArray("matchdata");

                JSONObject goal = openbuli.getJSONObject(0);
                JSONArray goals = goal.getJSONObject("goals").getJSONArray("goal");          

                 // loop array
                for(int i=0;i<goals.length();i++) {    
                                JSONObject e = goals.getJSONObject(i);
                                Log.e("Name", e.getString("goal_getter_name").toString());
                        }
于 2013-08-20T12:38:18.453 回答