繁体   English   中英

如何从 django rest api 获得结果?

[英]how get results from django rest api?

如何从 django rest api 获得结果?

https://pastebin.com/nsA3xRtr

https://pastebin.com/QWTGaYAF

https://pastebin.com/hDwCBPd7

如果我禁用分页,它的工作

这 api

https://igbaria.pythonanywhere.com/api/test/city/

您应该如下创建一个 class 来解析这个 JSON

public class Response{


    @SerializedName("next")
    String next;
    @SerializedName("previous")
    String previous;
    @SerializedName("count")
    int count;
    @SerializedName("count")
    int count;

    @SerializedName("results")
    ArrayList<City> results;
}

发生错误是因为您的 map json 到 java class 不正确。 使用这个:

import java.util.List;
import com.google.gson.annotations.SerializedName;

public class CityListItemResponse {

    @SerializedName("count")
    public Integer count;
    @SerializedName("next")
    public String next;
    @SerializedName("previous")
    public Object previous;
    @SerializedName("results")
    public List<City> results = null;
}


import com.google.gson.annotations.SerializedName;

public class City {

    @SerializedName("id")
    public Integer id;
    @SerializedName("title")
    public String title;
}

为了正确创建数据类,我建议你使用这个仪器链接

只需 select 您的选项粘贴 JSON 并查看预览

暂无
暂无

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

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