1

I have a Problem to access json data in a multi dimensional array. https://openligadb-json.heroku.com/api/matchdata_by_group_league_saison?group_order_id=20&league_saison=2010&league_shortcut=bl1

My Code works fine for the getJSONArray("matchdata");

But I can not access matchdata->match_results->match_result[0]->result_name

or

matchdata->goals->goal[0]->goal_getter_name

Here is my Code:

JSONObject json = null;
           json = jsonFunctions.getJSONfromURL("http://openligadb-json.heroku.com/api/matchdata_by_group_league_saison?group_order_id="+ group_order_id +"&league_saison="+ league_saison +"&league_shortcut=" + league_shortcut);     
        if (json != null){

      try{
        JSONArray  openbuli = null;
        openbuli = json.getJSONArray("matchdata");
        mylist.clear();

            for(int i=0;i<openbuli.length();i++){                       
                HashMap<String, String> map = new HashMap<String, String>();    
                JSONObject e = openbuli.getJSONObject(i);



                map.put("id",  String.valueOf(i));
                map.put("group_name", e.getString("group_name"));
                map.put("name_team1", e.getString("name_team1"));
                map.put("name_team2", e.getString("name_team2"));
                map.put("points_team1", e.getString("points_team1"));
                map.put("points_team2", e.getString("points_team2"));
                map.put("halbzeit", e.getString("result_name"));
                map.put("first", e.getString("name_team1") + " : " + e.getString("name_team2") + "  " + points_team1 + " : " + points_team2);
                map.put("second", e.getString("match_date_time"));
                mylist.add(map);
            }       
      }

      catch(JSONException e){
         Log.e("log_tag", "Error parsing data "+e.toString());
      }
      }

How can I access eg. matchdata->match_results->match_result[0]->result_name

Here the json data:

$json (
|    matchdata => Array (9)
|    (
|    |    ['0'] (
|    |    |    league_name =  "1. Fussball-Bundesliga 2010/2011"
|    |    |    match_is_finished =  TRUE
|    |    |    location (
|    |    |    |    location_id =  "0"
|    |    |    )
|    |    |    match_date_time_utc =  "2011-01-28T19:30:00+00:00"
|    |    |    id_team2 =  "55"
|    |    |    league_saison =  "2010"
|    |    |    match_results (
|    |    |    |    match_result => Array (2)
|    |    |    |    (
|    |    |    |    |    ['0'] (
|    |    |    |    |    |    result_name =  "Endergebnis"
|    |    |    |    |    |    result_order_id =  "1"
|    |    |    |    |    |    result_type_name =  "nach 90 Minuten"
|    |    |    |    |    |    points_team1 =  "2"
|    |    |    |    |    |    result_type_id =  "2"
|    |    |    |    |    |    points_team2 =  "0"
|    |    |    |    |    )
|    |    |    |    |    ['1'] (
|    |    |    |    |    |    result_name =  "Halbzeit"
|    |    |    |    |    |    result_order_id =  "2"
|    |    |    |    |    |    result_type_name =  "Halbzeit"
|    |    |    |    |    |    points_team1 =  "2"
|    |    |    |    |    |    result_type_id =  "1"
|    |    |    |    |    |    points_team2 =  "0"
|    |    |    |    |    )
|    |    |    |    )
|    |    |    )
|    |    |    group_id =  "294"
|    |    |    icon_url_team1 =  "http://www.openligadb.de/images/teamicons/Bayer_Leverkusen.gif"
|    |    |    league_shortcut =  "bl1"
|    |    |    goals (
|    |    |    |    goal => Array (2)
|    |    |    |    (
|    |    |    |    |    ['0'] (
|    |    |    |    |    |    goal_id =  "4367"
|    |    |    |    |    |    goal_getter_name =  "Vidal"
|    |    |    |    |    |    goal_mach_id =  "10171"
|    |    |    |    |    |    goal_penalty =  FALSE
|    |    |    |    |    |    goal_score_team1 =  "1"
|    |    |    |    |    |    goal_own_goal =  FALSE
|    |    |    |    |    |    goal_score_team2 =  "0"
|    |    |    |    |    |    goal_overtime =  FALSE
|    |    |    |    |    |    goal_match_minute =  "21"
|    |    |    |    |    |    goal_comment =  NULL
|    |    |    |    |    |    goal_getter_id =  "1135"
|    |    |    |    |    )
|    |    |    |    |    ['1'] (
|    |    |    |    |    |    goal_id =  "4368"
|    |    |    |    |    |    goal_getter_name =  "Rolfes"
|    |    |    |    |    |    goal_mach_id =  "10171"
|    |    |    |    |    |    goal_penalty =  FALSE
|    |    |    |    |    |    goal_score_team1 =  "2"
|    |    |    |    |    |    goal_own_goal =  FALSE
|    |    |    |    |    |    goal_score_team2 =  "0"
|    |    |    |    |    |    goal_overtime =  FALSE
|    |    |    |    |    |    goal_match_minute =  "42"
|    |    |    |    |    |    goal_comment =  NULL
|    |    |    |    |    |    goal_getter_id =  "511"
|    |    |    |    |    )
|    |    |    |    )
|    |    |    )
1
  • can you provide the response JSON string ..... Commented Jul 25, 2013 at 10:15

2 Answers 2

2

I know it should be in comment rather than answer but i am unable to comment due to reputations so i am putting it in to answer.

You can use gson library to parse your JSON response.

Please provide your JSON response so that it will be easier to understand.

Sign up to request clarification or add additional context in comments.

Comments

0
    private static String readMultidimensionalArray(JsonReader reader) throws IOException {
    StringWriter sw = new StringWriter();
    JsonWriter jw = new JsonWriter(sw);
    int arrayStack = 0;
    do {
        JsonToken t = reader.peek();
        switch (t) {
            case BEGIN_ARRAY:
                reader.beginArray();
                jw.beginArray();
                arrayStack++;
                break;
            case END_ARRAY:
                reader.endArray();
                jw.endArray();
                arrayStack--;
                break;
            case NUMBER:
                double d = reader.nextDouble();
                jw.value(d);
                break;
            default:
                break;
        }

    } while (arrayStack != 0);

    jw.flush();
    jw.close();
    return sw.toString();
}

This code just return the array as json string, but it can be adapted to any required logic.

Example output: [[1.0,0.9,0.0,0.0],[0.9,0.8,0.0,0.0],[0.0,0.0,0.3,0.5],[0.0,0.0,0.5,0.7]]

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.