JSON 内的改造列表字段不起作用

Retrofit list field inside JSON not working

我正在使用最新版本的改装 (2.9.0) 从 API 接收数据,数据结构(它们具有 getter 和 setter):

FamilyResponse.Java

@SerializedName("codigo")
@Expose
private String codigo;

@SerializedName("estado")
@Expose
private String estado;

@Expose
@SerializedName("List")
private List<Family> list;

Family.java

@SerializedName("idFampr")
@Expose
private String idFamilia;

@SerializedName("nomFam")
@Expose
private String nombre;

MainActivity.java

       Call<FamilyResponse> call = ApiClient.getInstance().getApi().getFamiliaProductos(new FamilyRequest("1"));

    call.enqueue(new Callback<FamilyResponse>() {
        @Override
        public void onResponse(Call<FamilyResponse> call, Response<FamilyResponse> response) {
            FamilyResponse familyResponse = response.body();
            if(familyResponse!=null)
            {
                for(Family family:familyResponse.getList()){
                Log.d("Familia",family.getNombre());
                }
            }
        }

        @Override
        public void onFailure(Call<FamilyResponse> call, Throwable t) {

        }
    });

我收到以下信息:

{
"codigo": "1",
"estado": "Consulta satisfactoria",
"list": [
    {
        "idFampr": 1,
        "nomFam": "Category 1",
        "icoFam": "img1.jpg",
        "ordFan": 1
    },
    {
        "idFampr": 2,
        "nomFam": "Category 2",
        "icoFam": "img2.jpg",
        "ordFan": 2
    },
    {
        "idFampr": 3,
        "nomFam": "Category 3",
        "icoFam": "img3.jpg",
        "ordFan": 3
    },
    {
        "idFampr": 4,
        "nomFam": "Category 4",
        "icoFam": "img4.jpg",
        "ordFan": 4
    },
    {
        "idFampr": 5,
        "nomFam": "Category 5",
        "icoFam": "img5.jpg",
        "ordFan": 5
    }
]

}

当我尝试读取 response.body() 时,“列表”字段为空,但 estado 和 codigo 具有正确的值

请在 FamilyResponse.Java class 中将您的 @SerializedName("List") 更改为 @SerializedName("list")

@Expose
@SerializedName("list")
private List<Family> list;

更多信息,请查看documentation

把你的@SerializedName("List")改成@SerializedName("list"),名字值必须和你的JSON属性一样

NOTE: The value you specify in this annotation must be a valid JSON field name.

Check documentation