test123123
test123123

Reputation: 921

JSON Parse shows strange behaviour

hy!

I have a Json String:

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

for a better understanding past the code in http://json.parser.online.fr/

and i parse it to a List of Entry Objects(SPEntry):

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);

                    }

                }

My Problem is that the picture setting shows a strange behaviour. In the second lessons element i don't get a picture and in all other cases i only get the "Entfall" picture. the pictures in the resourses are different

Please help

Screenshot of the list:

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

Upvotes: 0

Views: 236

Answers (2)

nicholas.hauschild
nicholas.hauschild

Reputation: 42849

Of the 7 lesson entries in your json string, only one of them is used once, and that is Verlegtvon (which also happens to be the second entry). I imagine there is something wrong with the drawable R.drawable.verlegt.

Upvotes: 1

Yashwanth Kumar
Yashwanth Kumar

Reputation: 29131

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);
                        }

If you look at the code carefully, finally you would end up with 2 images, either empty or verlegtvon , use else if to solve the problem. try the code below, i just added 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);
                        }

Upvotes: 4

Related Questions