2014-01-05 48 views
1

幫我解析JSON。我解析這個沒有「時刻」的JSON,不知道如何用「時刻」解析。 我的JSON響應:Android解析數組中的JSON

{ 
"A": [ 
    { 
     "time0_90": "20", 
     "score": "0 : 0", 
     "team1": "Россия", 
     "team2": "Франция", 
     "group": "A", 
     "live": "1" 
    }, 
    { 
     "time0_90": "20", 
     "score": "0 : 0", 
     "team1": "Португалия", 
     "team2": "Гондурас", 
     "group": "A", 
     "live": "0", 
     "time": "18:30", 
     "stadium": "", 
     "referee": "судья" 
    } 
], 
"B": [ 
    { 
     "time0_90": "3", 
     "score": "1 : 0", 
     "moment": [ 
      { 
       "class_moment": "g", 
       "name1": "Халк", 
       "time0_90Moment": "5", 
       "team": "1" 
      }, 
      { 
       "class_moment": "sub", 
       "name1": "Фред", 
       "time0_90Moment": "50", 
       "team": "1", 
       "name2": "Жо" 
      } 
     ], 
     "team1": "Бразилия", 
     "team2": "Испания", 
     "group": "B", 
     "live": "1" 
    } 
], 
"C": [], 
"D": [], 
"E": [], 
"F": [ 
    { 
     "time": "15:00", 
     "stadium": "Маракана", 
     "referee": "судья", 
     "team1": "Россия", 
     "team2": "Франция", 
     "group": "F", 
     "live": "0" 
    } 
], 
"G": [], 
"H": [] 
} 

這段代碼是沒有 「時刻」 解析響應。如何用「時刻」解析響應並將其保存在數組中?

JSONObject jsonResponse = new JSONObject(jsonResult); 
    for (int j=0; j<8; j++){ 
     String[] groupName = {"A", "B", "C", "D", "E", "F", "G", "H"}; 
    JSONArray jsonMainNode = jsonResponse.optJSONArray(groupName[j]); 
    if (jsonMainNode.length() == 0){ continue;} 
    else { 
    for (int i = 0; i < jsonMainNode.length(); i++) { 
    JSONObject jsonChildNode = jsonMainNode.getJSONObject(i); 
    team1 = jsonChildNode.optString("team1"); 
    team2 = jsonChildNode.optString("team2");  
    group = jsonChildNode.optString("group"); 
    int live = jsonChildNode.getInt("live"); 
    String time = null; 
    if (live == 1){ 
     time = jsonChildNode.optString("time0_90")+"'"; 
     score = jsonChildNode.optString("score"); 
     } 
     else { 
      score = jsonChildNode.optString("time"); 
      referee = jsonChildNode.optString("referee"); 
      time = jsonChildNode.optString("stadium"); 
      } 

回答

1

試試這個:

jsonArray moment = json.getJsonArray("B").getJsonObject(0).getJsonArray("moment"); 
String class = moment.getString("class_moment"); 
String name= moment.getString("name1"); 
String time= moment.getString("time0_90Moment"); 
String team= moment.getString("team"); 
1

我會建議你使用JSON映射庫,像JacksonGSON,手動解析JSON的是時間和精力的浪費,除非它純粹是爲了教育目的。

除此之外,我認爲正確的用法是.optJSONArray(「時刻」),通過每個結果的強制轉換爲JSONObject的或類似的迭代跟進?手動做這個只是一團糟。 ;○

0

我做到了,這裏的代碼

JSONObject jsonResponse = new JSONObject(jsonResult); 
     for (int j=0; j<8; j++){ 
      String[] groupName = {"A", "B", "C", "D", "E", "F", "G", "H"}; 
     JSONArray jsonMainNode = jsonResponse.optJSONArray(groupName[j]); 
     if (jsonMainNode.length() == 0){ continue;} 
     else { 
     for (int i = 0; i < jsonMainNode.length(); i++) { 
     JSONObject jsonChildNode = jsonMainNode.getJSONObject(i); 
     team1 = jsonChildNode.optString("team1"); 
     team2 = jsonChildNode.optString("team2");  
     group = jsonChildNode.optString("group"); 
     int live = jsonChildNode.getInt("live"); 
     String time = null; 
     if (live == 1){ 
      time = jsonChildNode.optString("time0_90")+"'"; 
      score = jsonChildNode.optString("score"); 
      JSONArray jsonMoment = jsonChildNode.optJSONArray("moment"); 
      if (jsonMoment == null) 
       { 
       class_moment=new String[1]; 
       name1 = new String[1]; 
       name2 = new String[1]; 
       name1 = new String[1]; 
       time0_90Moment = new String[1]; 
       team = new int[1]; 
       } 
       else { 
        class_moment=new String[jsonMoment.length()]; 
        name1 = new String[jsonMoment.length()]; 
        name2 = new String[jsonMoment.length()]; 
        name1 = new String[jsonMoment.length()]; 
        time0_90Moment = new String[jsonMoment.length()]; 
        team = new int[jsonMoment.length()]; 
      for (int k = 0; k < jsonMoment.length(); k++) { 
       JSONObject jsonChildMoment = jsonMoment.getJSONObject(k);    
       class_moment[k] = jsonChildMoment.optString("class_moment");     
       name1[k] = jsonChildMoment.optString("name1"); 
       name2[k] = jsonChildMoment.optString("name2"); 
       time0_90Moment[k] = jsonChildMoment.optString("time0_90Moment"); 
       team[k] = jsonChildMoment.getInt("team");    
      }}   
      } 
      else { 
       score = jsonChildNode.optString("time"); 
       referee = jsonChildNode.optString("referee"); 
       time = jsonChildNode.optString("stadium"); 
       } 
+0

看來你確實或多或少是我寫的使用optJSONArray和使用的JSONObject迭代+。接受答案會很好。除此之外,使用映射器仍然非常優越,並且與編寫該代碼相比將花費大約1/100的時間。 –