2011-10-18 2 views
0

hy! http://json.parser.online.fr/JSON 구문 분석이 이상한 동작을 나타냅니다

의 코드 과거의 더 나은 이해를위한

{"responseData":{"days":[{"date":1289430000,"lessons":[{"lesson":"3","classname":"XXXX","oldTeacher":"RUMET","newTeacher":"JAKOB","oldSubject":"0AM","newSubject":"0AM","oldRoom":"104","newRoom":"104 ","comment":""},{"lesson":"4","classname":"XXXX","oldTeacher":"RUMET","newTeacher":"JAKOB","oldSubject":"0AM","newSubject":"0APH","oldRoom":"104","newRoom":"107 ","comment":"Verlegtvon"},{"lesson":"8","classname":"XXXX","oldTeacher":"JAKOB","newTeacher":"","oldSubject":"0APH","newSubject":"","oldRoom":"107","newRoom":" ","comment":"Entfall"}]},{"date":1289516400,"lessons":[{"lesson":"1","classname":"XXXX","oldTeacher":"KAIS","newTeacher":"","oldSubject":"0RW1","newSubject":"","oldRoom":"107","newRoom":" ","comment":"Entfall"},{"lesson":"2","classname":"XXXX","oldTeacher":"KAIS","newTeacher":"TRAUN","oldSubject":"0RW1","newSubject":"0BO","oldRoom":"107","newRoom":"107 ","comment":""}]},{"date":1289948400,"lessons":[{"lesson":"5","classname":"XXXX","oldTeacher":"KIES","newTeacher":"","oldSubject":"0RK","newSubject":"","oldRoom":"107","newRoom":" ","comment":"Entfall"}]},{"date":1290121200,"lessons":[{"lesson":"6","classname":"XXXX","oldTeacher":"KIES","newTeacher":"","oldSubject":"0RK","newSubject":"","oldRoom":"107","newRoom":" ","comment":"Entfall"}]}]},"responseDetails":null,"responseStatus":200} 

그리고 난 항목 개체의 목록 (SPEntry)로 구문 분석 :

은 내가 JSON 문자열을

public class EntryParse{ 

    ArrayList<SPEntry> list; 
    public EntryParse(Context ctx, String par_json) 
    { 
     try 
     { 
      list = new ArrayList<SPEntry>(); 
       JSONArray array = new JSONArray(par_json); 

       for (int i = 0; i < array.length(); i++) {    //Datum 
        JSONObject json = array.getJSONObject(i); 

        Date date = new Date(json.getLong("date")*1000); 
        SimpleDateFormat ft = new SimpleDateFormat ("dd.MM.yyyy"); 

        JSONArray lessons = json.getJSONArray("lessons"); 

        for (int j = 0; j < lessons.length(); j++) {  //Stunden 

         JSONObject obj = lessons.getJSONObject(j); 
         SPEntry entry = new SPEntry(); 
         entry.date = ft.format(date); 
         entry.lesson = obj.optString("lesson"); 
         entry.teacher = obj.optString("oldTeacher"); 
         entry.newTeacher = obj.optString("newTeacher"); 
         entry.lesson = obj.optString("oldSubject"); 
         entry.newlesson = obj.optString("newSubject"); 
         entry.oldRoom = obj.optString("oldRoom"); 
         entry.newRoom = obj.optString("newRoom"); 
         entry.comment = obj.optString("comment"); 
         if(entry.comment.equals("Entfall")){ 
          entry.picture = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.entfall); 
         } 
         if(entry.comment.equals("Betreuung")){ 
          entry.picture = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.betreung); 
         } 
         if(entry.comment.equals("Verlegtvon")){ 
          entry.picture = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.verlegt); 
         } 
         else 
         { 
          entry.picture = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.empty); 
         } 


         list.add(entry); 

        } 

       } 

내 문제는 그림 설정이 이상한 동작을 보여줍니다. 두 번째 레슨 요소에서는 그림을 얻지 못하고 다른 모든 경우에는 "Entfall"그림 만 얻습니다. 의 resourses의 사진

목록의

스크린 샷 도와주세요 다르다 : 당신의 JSON 문자열에서 7 개 교훈 항목의

http://img6.imagebanana.com/img/fnmedlrr/device20111018181454.png

+2

넣어이 경우 (entry.comment.equals ("Verlegtvon")) {entry.picture = BitmapFactory.decodeResource (ctx.getResources(), R.drawable.entfall 추가); } 두 번째 요소에 대해서도 동일한 이미지를 얻는 지 확인하십시오. – user370305

+0

과 Yashwanth Kumar는 단순한 경우 대신에 다른 경우에 적합합니다. – user370305

+0

두 번째 요소 (목록 행)에 동일한 이미지가있는 경우 drawable을 가져 오는 과정이 생깁니다. – user370305

답변

4
if(entry.comment.equals("Entfall")){ 
          entry.picture = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.entfall); 
         } 
         if(entry.comment.equals("Betreuung")){ 
          entry.picture = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.betreung); 
         } 
         if(entry.comment.equals("Verlegtvon")){ 
          entry.picture = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.verlegt); 
         } 
         else 
         { 
          entry.picture = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.empty); 
         } 

주의 깊게 코드를 보면, 결국 당신은 문제를 해결하는 경우 다른 사용, 2 개 이미지 중 하나 empty 또는 verlegtvon로 끝날 것입니다. 아래의 코드를 시도, 난 그냥 else if

if(entry.comment.equals("Entfall")){ 
          entry.picture = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.entfall); 
         } 
         else if(entry.comment.equals("Betreuung")){ 
          entry.picture = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.betreung); 
         } 
         else if(entry.comment.equals("Verlegtvon")){ 
          entry.picture = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.verlegt); 
         } 
         else 
         { 
          entry.picture = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.empty); 
         } 
+0

hy는 이미 if else 부분을 가지고 있으며 두 번째 이미지가 – test123123

+0

인 경우 listView를 채우는 것과 관련된 문제가 있습니다. 목록보기. 이와 비슷한 질문을하면 문제가 해결됩니다. –

+0

내 솔루션 .... thx를 게시했습니다. – test123123

1

는, 그 중 하나가 한 번 사용됩니다, 그리고 그것은 Verlegtvon입니다 (두 번째 항목이기도합니다). 드로어 블 R.drawable.verlegt에 문제가 있다고 생각합니다.

+0

if 조건을 변경했을 때 문제가 발생하지 않습니다 – test123123

관련 문제