簡體   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