【问题标题】:JSON to POJO conversion error for different types of values不同类型值的 JSON 到 POJO 转换错误
【发布时间】: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


    【解决方案1】:

    先定义一个值类

    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;
        }
    }
    

    然后更新你的大根 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;
        }
    }
    

    并使用 Gson 进行转换

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

    【讨论】:

      猜你喜欢
      • 2014-04-17
      • 2021-05-02
      • 1970-01-01
      • 2017-09-08
      • 1970-01-01
      • 1970-01-01
      • 2015-12-04
      • 2017-11-25
      • 1970-01-01
      相关资源
      最近更新 更多