JSON to POJO conversion error for different types of values

730 views Asked by At

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?

1

There are 1 answers

0
bhdrkn On

First define a value class

class Value {
    private String type;
    private String value;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getValue() {
        return value;
    }

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

Then update your big root pojo

class Pojo {
    private String htmlId;
    private Collection<Value> valuea;
    private String contentType;

    public String getHtmlId() {
        return htmlId;
    }

    public void setHtmlId(String htmlId) {
        this.htmlId = htmlId;
    }

    public Collection<Value> getValuea() {
        return valuea;
    }

    public void setValuea(Collection<Value> valuea) {
        this.valuea = valuea;
    }

    public String getContentType() {
        return contentType;
    }

    public void setContentType(String contentType) {
        this.contentType = contentType;
    }
}

And convert it using Gson

    GsonBuilder builder = new GsonBuilder();
    Gson gson = builder.create();
    final Collection<Pojo> collection = gson.fromJson(json, Collection.class);