加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – Json映射异常不能将实例反序列化为START_ARRAY令牌

发布时间:2020-12-14 05:13:59 所属栏目:Java 来源:网络整理
导读:我试图解析我的json请求到我的模型.我不知道这段代码有什么问题. json的语法对 Java模型也是正确的和注释.我不知道为什么我会收到错误: Caused by: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of ParametersType out of
我试图解析我的json请求到我的模型.我不知道这段代码有什么问题. json的语法对 Java模型也是正确的和注释.我不知道为什么我会收到错误:
Caused by: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of ParametersType out of START_ARRAY token
(through reference chain: Document["parameters"])

Java模型:

@JsonIgnoreProperties( ignoreUnknown = true )
public class Document {

@XmlElement( required = true )
@JsonProperty( "templateId" )
protected String templateId;

@JsonProperty( "parameters" )
@XmlElement( required = true )
protected ParametersType parameters;

@JsonProperty( "documentFormat" )
@XmlElement( required = true )
protected DocumentFormatType documentFormat;

@JsonIgnoreProperties( ignoreUnknown = true )
public class ParametersType {

    @JsonProperty( "parameter" )
    protected List<ParameterType> parameter;

@JsonIgnoreProperties( ignoreUnknown = true )
public class ParameterType {

    @XmlElement( required = true )
    @JsonProperty( "key" )
    protected String key;

    @XmlElement( required = true )
    @JsonProperty( "value" )
    @XmlSchemaType( name = "anySimpleType" )
    protected Object value;

    @JsonProperty( "type" )
    @XmlElement( required = true,defaultValue = "STRING_TYPE" )
    protected ParamType type;

杰森代码:

{
    "templateId": "123","parameters": [
        {
            "parameter": [
                {
                    "key": "id","value": "1","type": "STRING_TYPE"
                },{
                    "key": "id2","value": "12","type": "STRING_TYPE"
                }
            ]
        }
    ],"documentFormat": "PDF"
}

解决方法

您将参数声明为单个对象,但您将其作为JSON文档中的多个对象的数组返回.

您的模型目前将参数节点定义为ParametersType对象:

@JsonProperty( "parameters" )
@XmlElement( required = true )
protected ParametersType parameters;

这意味着你的模型对象期待一个JSON文档,如下所示:

{
    "templateId": "123","parameters": {
            "parameter": [
                {
                    "key": "id","type": "STRING_TYPE"
                }
            ]
        },"documentFormat": "PDF"
}

但是在您的JSON文档中,您返回了一个ParametersType对象的数组.因此,您需要将模型更改为ParametersType对象的列表:

@JsonProperty( "parameters" )
@XmlElement( required = true )
protected List<ParametersType> parameters;

事实上,你返回一个ParametersType对象的数组是为什么解析器抱怨无法反序列化一个对象在START_ARRAY之外.它正在寻找一个具有单个对象的节点,但在JSON中找到了一个对象数组.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读