android json多维数组

时间:2013-07-25 10:13:28

标签: android json parsing

我有一个问题是访问多维数组中的json数据。 https://openligadb-json.heroku.com/api/matchdata_by_group_league_saison?group_order_id=20&league_saison=2010&league_shortcut=bl1

我的代码适用于getJSONArray(“matchdata”);

但我无法访问matchdata-> match_results-> match_result [0] - > result_name

matchdata-> goals->目标[0] - > goal_getter_name

这是我的代码:

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

我如何访问例如。 matchdata-&GT; match_results-&GT; match_result [0] - &GT; result_name

这里是json数据:

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

2 个答案:

答案 0 :(得分:2)

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

您可以使用gson库来解析您的JSON回复。

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

答案 1 :(得分: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]]