2014-01-05 2 views
1

JSON 구문 분석을 도와주세요. 이 JSON을 "순간"없이 구문 분석하고 "순간"을 구문 분석하는 방법을 모릅니다. 내 JSON 응답 :어레이에서 배열로 JSON을 파싱하는 Android

{ 
"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

난 당신이 Jackson 또는 GSON 같은 JSON 매핑 라이브러리를 사용하는 것이 좋습니다 것입니다 수동 구문 분석 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의 시간이 소요됩니다. –

관련 문제