Json Mapping Exception无法反序列化START_ARRAY令牌中的实例 [英] Json Mapping Exception can not deserialize instance out of START_ARRAY token

查看:161
本文介绍了Json Mapping Exception无法反序列化START_ARRAY令牌中的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的json请求解析为我的模型。我不知道这段代码有什么问题。 json的语法在Java模型上看起来也是正确的和注释。我不知道为什么我会收到如下错误:

I'm trying to parse my json request to my model. I dunno what is wrong in this code. Syntax of json looks correct and annotations on Java model also. I don't know why I'm getting error like:

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;

....}

Json代码:

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


推荐答案

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

You have declared parameters as a single object, but you are returning it as an array of multiple objects in your JSON document.

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

Your model currently defines the parameters node as a ParametersType object:

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

这意味着您的模型对象需要一个如下所示的JSON文档:

This means your model object is expecting a JSON document that looks like the following:

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

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

But in your JSON document you are returning an array of ParametersType objects. So you need to change your model to be a list of ParametersType objects:

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

您返回一个ParametersType对象数组的事实是解析器抱怨无法使用的原因从START_ARRAY反序列化对象。它正在寻找具有单个对象的节点,但在JSON中找到了一个对象数组。

The fact that you are returning an array of ParametersType objects is why the parser is complaining about not being able to deserialize an object out of START_ARRAY. It was looking for a node with a single object, but found an array of objects in your JSON.

这篇关于Json Mapping Exception无法反序列化START_ARRAY令牌中的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆