While converting JSON data to POJO using Gson I get this error.
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 119
My JSON is :
{
"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"
}
]
}
for each result the value type may differ. Sometimes it is a string value or an array.
Here's my pojo for results:
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;
}
When there is no snippet type in the result, my code works fine. Tried with typecast, that did not help me either.
What can be the best way to handle such scenario?
First define a value class
Then update your big root pojo
And convert it using Gson