0

I do not find my error. Eclipse tell me: The method getJSONObject(int) in the type JSONArray is not applicable for the arguments (String) I want to access: matchdata -> goals -> goal -> goal_getter_name The eclipse error is on this line: `JSONObject goal = openbuli.getJSONObject("goal"); I think the variable openbuli ist wrong?! How I have to fix it?

Here ist my Code:

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

Here the json data:

$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
|    |    |    |    |    )
|    |    |    |    )
|    |    |    )
|    |    )
1
  • 1
    You can't do this openbuli.getJSONObject("goal"); you have to give that method an index of the array. As example: openbuli.getJSONObject(1); Commented Aug 20, 2013 at 12:26

1 Answer 1

1

you need to change openbuli.getJSONObject("goal"); to openbuli.getJSONObject(0);
and add getJSONObject("goals") to get the object goals from the first place in the array

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());
                        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.