2015-04-07 137 views
1

我解析json列表視圖..數據解析很好,但是在數組適配器類中存在一些問題,因爲它沒有相應地顯示視圖。列表視圖不顯示相應的數據,當解析json

public void parseJson(JSONObject json) { 
    try { 
      JSONArray posts = json.getJSONArray("resume"); 
      feedList = new ArrayList<JsonItem>(); 
      for (int i = 0; i < posts.length(); i++) { 
       JSONObject post = (JSONObject) posts.getJSONObject(i); 
       JsonItem item = new JsonItem(); 
       JSONArray cast = post.getJSONArray("Education"); 
       Log.i("lenght", Integer.toString(cast.length())); 
       if (cast != null && cast.length()>0) { 
        for(int j =0 ;j<cast.length();j++){ 
        JSONObject casts = (JSONObject)cast.getJSONObject(j); 
         item.setTitle(casts.getString("Title")); 
         feedList.add(item); 
         Log.i("feedsize", feedList.get(j).toString()); 
       } 
      } 
     } 
    }catch(JSONException e){ 
     e.printStackTrace(); 
} 

JSON數組:

{ 
    "resume":[ 
     { 
      "Education":[ 
       { 
        "Title":"Post Graduation", 
        "Degree":"M. Tech." 
       }, 
       { 
        "Title":"Graduation", 
        "Degree":"B. Tech." 
       } 
      ] 
     } 
    ] 
} 

我這個數組有兩個頭銜:畢業後畢業

但在佈局出它顯示只有一個值:畢業

output

回答

1

實例化內部循環,而不是外部,這樣您JsonItem對象:

public void parseJson(JSONObject json) { 
try { 
     JSONArray posts = json.getJSONArray("resume"); 
     feedList = new ArrayList<JsonItem>(); 
     for (int i = 0; i < posts.length(); i++) { 
      JSONObject post = (JSONObject) posts.getJSONObject(i); 

      JSONArray cast = post.getJSONArray("Education"); 
      Log.i("lenght", Integer.toString(cast.length())); 
      if (cast != null && cast.length()>0) { 
       for(int j =0 ;j<cast.length();j++){      

        JsonItem item = new JsonItem(); 

        JSONObject casts = (JSONObject)cast.getJSONObject(j); 
        item.setTitle(casts.getString("Title")); 
        feedList.add(item); 
        Log.i("feedsize", feedList.get(j).toString()); 
       } 
      } 


     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
} 
+0

太感謝你了.. – Mohit

相關問題