解析JSON這與前一個問題,我在這裏問早些時候使用枚舉而與GSON
我試圖解析相同的JSON,但現在我已經改變了我班一點點。
{
"lower": 20,
"upper": 40,
"delimiter": " ",
"scope": ["${title}"]
}
我班現在看起來像:
public class TruncateElement {
private int lower;
private int upper;
private String delimiter;
private List<AttributeScope> scope;
// getters and setters
}
public enum AttributeScope {
TITLE("${title}"),
DESCRIPTION("${description}"),
private String scope;
AttributeScope(String scope) {
this.scope = scope;
}
public String getScope() {
return this.scope;
}
}
此代碼拋出一個異常,
com.google.gson.JsonParseException: The JsonDeserializer EnumTypeAdapter failed to deserialized json object "${title}" given the type class com.amazon.seo.attribute.template.parse.data.AttributeScope
at
唯一的例外是可以理解的,因爲每個解決我剛纔的問題是,GSON是預計Enum對象實際上被創建爲
${title}("${title}"),
${description}("${description}");
但由於這在語法上是不可能的,因此建議的解決方案是什麼?
非常感謝。這工作。 –
它的工作原理!謝謝!! –