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

2 回答 2

2

我知道它应该发表评论而不是回答,但由于声誉问题我无法发表评论,所以我将其放入回答中。

您可以使用gson库来解析您的JSON响应。

请提供您的JSON回复,以便更容易理解。

于 2013-07-25T10:29:59.713 回答
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();
}

此代码只是将数组作为 json 字符串返回,但它可以适应任何所需的逻辑。

示例输出:[[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]]

于 2015-10-15T18:31:55.530 回答