2013-07-25 103 views
1

我有一個問題,以訪問多維數組中的json數據。 https://openligadb-json.heroku.com/api/matchdata_by_group_league_saison?group_order_id=20&league_saison=2010&league_shortcut=bl1android json多維數組

我的代碼適用於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-> match_results-> match_result [0] - > 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" 
| | | | | ) 
| | | | ) 
| | | ) 
+0

你能提供答案JSON字符串..... –

+0

我更新了我的帖子 – sarahsdev

回答

2

我知道它應該在,而不是答案的評論,但我無法評論,由於聲譽,所以我我正在回答。

您可以使用gson庫來解析您的JSON響應。

請提供您的JSON響應,以便更容易理解。

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]]