简体   繁体   中英

Android - Retrofit json parsing

I have following json array. I want to parse it in android using retrofit

[
  {
    "todo_id": "1",
    "todo_content": "Homework",
    "date": "2016-05-05",
    "iscomplete": "1",
    "imagelink": "Lighthouse.jpg"
  },
  {
    "todo_id": "2",
    "todo_content": "exam",
    "date": "2015-04-21",
    "iscomplete": "0",
    "imagelink": "Desert.jpg"
  },
  {
    "todo_id": "3",
    "todo_content": "Lab report",
    "date": "2014-08-29",
    "iscomplete": "1",
    "imagelink": "FB_IMG_14700753538617403.jpg"
  }
]

when I'm using retrofit for json parsing but it do not parse todo_id and todo_content key values where as it show other key values date, iscomplete and imagelink This is my android code for retrofit response

 public void onResponse(Call<List<AndroidVersion>> call, Response<List<AndroidVersion>> response) {
                    if (response.isSuccess()) {
                        try {
                            for (int i = 0; i < response.body().size(); i++) {
                                AndroidVersion ver = new AndroidVersion();
                                ver.setTodoContent(response.body().get(i).getTodoContent());
                                ver.setTodoId(response.body().get(i).getTodoId());
                                ver.setDate(response.body().get(i).getDate());
                                ver.setIscomplete(response.body().get(i).getIscomplete());
                                ver.setImagelink(response.body().get(i).getImagelink());
                                arrayList.add(ver);
                                Log.v("ret",response.body().get(i).toString());
                            }
                            adapter = new DataAdapter(arrayList);
                            recyclerView.setAdapter(adapter);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                }

Use Serialized name.

Your json response is having parameter name "todo_id" so in your pojo class you have to mention it as it is, follow the example of the pojo class:

public class Pojo {
    private int todo_id;
    private String todo_content;

    public int getTodo_id() {
        return todo_id;
    }

    public void setTodo_id(int todo_id) {
        this.todo_id = todo_id;
    }

    public String getTodo_content() {
        return todo_content;
    }

    public void setTodo_content(String todo_content) {
        this.todo_content = todo_content;
    }

}

The names should match.. Hope this helps!

public class Example {

@SerializedName("todo_id")
@Expose
private String todoId;
@SerializedName("todo_content")
@Expose
private String todoContent;
@SerializedName("date")
@Expose
private String date;
@SerializedName("iscomplete")
@Expose
private String iscomplete;
@SerializedName("imagelink")
@Expose
private String imagelink;

public String getTodoId() {
return todoId;
}

public void setTodoId(String todoId) {
this.todoId = todoId;
}

public String getTodoContent() {
return todoContent;
}

public void setTodoContent(String todoContent) {
this.todoContent = todoContent;
}

public String getDate() {
return date;
}

public void setDate(String date) {
this.date = date;
}

public String getIscomplete() {
return iscomplete;
}

public void setIscomplete(String iscomplete) {
this.iscomplete = iscomplete;
}

public String getImagelink() {
return imagelink;
}

public void setImagelink(String imagelink) {
this.imagelink = imagelink;
}

}

It should work for you

Go to this link and paste your response there to generate appropriate POJO class for your response. This will generate getter and setter methods as well. Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM