簡體   English   中英

如何解析一個JSON,其中對象有時充當Retrofit中的字符串或數組

[英]How to parse a json in which the object sometime acts as a string or an array in Retrofit

我曾經在Volley上做過,但我想在Retrofit中做。 我也在堆棧溢出中搜索,但是解決方案有點混亂。 以下是json。

[{
        "attribute_code": "color",
        "value": "49"
    },
    {
        "attribute_code": "category_ids",
        "value": [
            "3",
            "4"
        ]
    },
    {
        "attribute_code": "options_container",
        "value": "container2"
    }]

以下是POJO類。

public class ModelCustomAttrRes {
@SerializedName("attribute_code")
@Expose
private String attributeCode;
@SerializedName("value")
@Expose
private String value;

public String getAttributeCode() {
    return attributeCode;
}

public void setAttributeCode(String attributeCode) {
    this.attributeCode = attributeCode;
}

public String getValue() {
    return value;
}

public void setValue(String value) {
    this.value = value;
}
}

更改Object中的String值,您的POJO將像這樣。我確定它將對您有用。 對象數據類型確實包含字符串和數組兩個值。

public class ModelCustomAttrRes {
@SerializedName("attribute_code")
@Expose
private String attributeCode;
@SerializedName("value")
@Expose
private Object value;

public String getAttributeCode() {
return attributeCode;
}

public void setAttributeCode(String attributeCode) {
this.attributeCode = attributeCode;
}

public Object getValue() {
return value;
}

public void setValue(Object value) {
this.value = value;
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM