【发布时间】:2015-09-04 00:21:49
【问题描述】:
使用 Gson 将 JSON 数据转换为 POJO 时出现此错误。
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: 应为 BEGIN_ARRAY 但为 STRING 在第 1 行第 119 列
我的 JSON 是:
{
"success":true,
"result":[
{
"htmlId":"edit_text_1",
"value":"3",
"contentType":"Snippet"
},
{
"htmlId":"edit_text_2",
"value":[
{
"type":"HTML",
"value":"<ul>\n<li>This is a text from the static editable content.</li>\n</ul>"
},
{
"type":"Text",
"value":"- This is a text from the static editable content."
} ],
"contentType":"Text"
}
]
}
对于每个结果,值类型可能不同。有时是字符串值或数组。
这是我对结果的看法:
private String htmlId;
private Object value = new ArrayList<Object>();
private String contentType;
public String getHtmlId() {
return htmlId;
}
public void setHtmlId(String htmlId) {
this.htmlId = htmlId;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
if(value instanceof String)
this.value = (List<String>)value;
else if(value instanceof ArrayList)
this.value = (ArrayList<MarketoTypeValue>)value;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
当结果中没有 sn-p 类型时,我的代码可以正常工作。 尝试使用类型转换,这也对我没有帮助。
处理这种情况的最佳方法是什么?
【问题讨论】:
标签: java json gson jsonschema2pojo