Json 映射异常无法从 START_ARRAY 令牌反序列化实例 [英] Json Mapping Exception can not deserialize instance out of START_ARRAY token

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

问题描述

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

I'm trying to parse my json request to my model. I don't know, 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"
}

推荐答案

您已将 parameters 声明为单个对象,但您将其作为 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 映射异常无法从 START_ARRAY 令牌反序列化实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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