如何使用改进来深入解析Json到gson

时间:2017-04-24 09:32:16

标签: json parsing retrofit2 android gson

我正在从我的Android应用程序中使用API​​,并且所有JSON响应都是这样的:

[{
    "id": 1,
    "title": "sell parm",
    "created_by": "7",
    "created_at": "2017-04-13T05:59:56.625Z",
    "updated_at": "2017-04-13T05:59:56.625Z",
    "items": [
      {
        "id": 1,
        "name": "item1",
        "done": null,
        "todo_id": 1,
        "created_at": "2017-04-13T06:31:18.136Z",
        "updated_at": "2017-04-13T06:31:18.136Z"
      }
    ]
  },
  {
    "id": 2,
    "title": "brobro",
    "created_by": "7",
    "created_at": "2017-04-13T06:02:51.855Z",
    "updated_at": "2017-04-13T06:02:51.855Z",
    "items": []
  },
  {
    "id": 3,
    "title": "brobro",
    "created_by": "7",
    "created_at": "2017-04-13T06:16:13.186Z",
    "updated_at": "2017-04-13T06:16:13.186Z",
    "items": []
  },
  {
    "id": 4,
    "title": "brsro",
    "created_by": "7",
    "created_at": "2017-04-13T06:17:14.515Z",
    "updated_at": "2017-04-13T06:17:14.515Z",
    "items": []
  }
]

问题是我所有的POJO都有一个Todo,项目,我想只有一个叫retorit2,Todo类列表。

有没有办法创建一个Gson的自定义转换器来提取Todo字段,所以改装返回合适的POJO?

对不起。我的英语不好......

1 个答案:

答案 0 :(得分:0)

@Hyungsik你可以做这样的事情(随意问任何问题)

  

创建一个由Retrofit2使用的接口或做一些像下面这样的添加

public interface ApiInterface {

    // Get Your Data
    @GET("Your_API_URL")
    Call<ArrayList<Pojo>> getMyData();
}
  

在您的应用中创建两个以下课程。

<强> Pojo.java

public class Pojo {

    private Integer id;
    private String title;
    private String createdBy;
    private String createdAt;
    private String updatedAt;
    private List<Item> items = null;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    public String getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(String createdAt) {
        this.createdAt = createdAt;
    }

    public String getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(String updatedAt) {
        this.updatedAt = updatedAt;
    }

    public List<Item> getItems() {
        return items;
    }

    public void setItems(List<Item> items) {
        this.items = items;
    }

}

Item.java

class Item {

    private Integer id;
    private String name;
    private Object done;
    private Integer todoId;
    private String createdAt;
    private String updatedAt;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Object getDone() {
        return done;
    }

    public void setDone(Object done) {
        this.done = done;
    }

    public Integer getTodoId() {
        return todoId;
    }

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

    public String getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(String createdAt) {
        this.createdAt = createdAt;
    }

    public String getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(String updatedAt) {
        this.updatedAt = updatedAt;
    }
}
  

您将能够非常轻松地使用这些类解析您的API数据。