Api返回对象而不是数组的对象

时间:2019-08-02 22:08:57

标签: java json retrofit retrofit2

我正在尝试使用Retrofit2和Gson与我无法控制的在线api进行交互。结果包含一个对象,该对象由与数组相对的其他对象组成。

返回的json片段如下:

{
"error": false,
"count": 116,
"info": "116 strains was found for your search.",
"strains": {
    "Master_Thai_-_Angels_Flight": {
        "name": "Angel's Flight",
        "brname": "Master Thai",
        "id": "Angels_Flight",
        "brid": "Master_Thai"
        },
    "DJ_Short_-_Azure_Haze": {
        "name": "Azure Haze",
        "brname": "DJ Short",
        "id": "Azure_Haze",
        "brid": "DJ_Short"
        },
    "MTG_Seeds_-_B-4": {
        "name": "B-4",
        "brname": "MTG Seeds",
        "id": "B-4",
        "brid": "MTG_Seeds"
        }
}
}

我试图创建一些类似的pojo:

data class StrainSearchResult(
    @SerializedName("error")
    @Expose
    val error: Boolean,
    @SerializedName("count")
    @Expose
    val count: String,
    @SerializedName("info")
    @Expose
    val info: String,
    @SerializedName("strains")
    @Expose
    val strains: Strains
)

data class Strains(
    @SerializedName("name")
    @Expose
    val strainName: String,
    @SerializedName("brname")
    @Expose
    val breederName: String,
    @SerializedName("id")
    @Expose
    val strainId: String,
    @SerializedName("brid")
    @Expose
    val breederId: String
)

如果我调用api,则响应对象如下:

StrainSearchResult:
count: # of results,
error: false,
info: "A string",
strains: A single object of Strain that lists all properties as null

See here for a picture

如何将“ strains” json对象解析为“ strains”列表?

0 个答案:

没有答案