简体   繁体   English

Android-翻新json解析

[英]Android - Retrofit json parsing

I have following json array. 我有以下JSON数组。 I want to parse it in android using retrofit 我想使用改造在android中解析它

[
  {
    "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 当我使用翻新进行json解析时,但不解析todo_id和todo_content的关键值,因为它显示了其他关键值的日期,不完整和imagelink,这是我的Android代码,用于做出响应

 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: 您的json响应具有参数名称“ todo_id”,因此在pojo类中,您必须按原样提及它,请遵循pojo类的示例:

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. 转到此链接并将您的响应粘贴到那里,以为您的响应生成适当的POJO类。 This will generate getter and setter methods as well. 这也将生成getter和setter方法。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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