微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我试图解析我的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中找到了一个对象数组.

原文地址:https://www.jb51.cc/java/123060.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐