程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Json Mapping Exception无法从START_ARRAY令牌中反序列化实例大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Json Mapping Exception无法从START_ARRAY令牌中反序列化实例?

开发过程中遇到Json Mapping Exception无法从START_ARRAY令牌中反序列化实例的问题如何解决?下面主要结合日常开发的经验,给出你关于Json Mapping Exception无法从START_ARRAY令牌中反序列化实例的解决方法建议,希望对你解决Json Mapping Exception无法从START_ARRAY令牌中反序列化实例有所启发或帮助;

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

您的模型当前将parameters节点定义为一个ParametersType对象:

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

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

{
    "templatEID": "123",
    "parameters": {
            "parameter": [
                {
                    "key": "ID",
                    "value": "1",
                    "type": "StriNG_TYPE"
                },
                {
                    "key": "ID2",
                    "value": "12",
                    "type": "StriNG_TYPE"
                }
            ]
        },
    "documentFormat": "pdf"
}

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

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

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

解决方法

我正在尝试将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" )
    @XmlscheR_401_11845@aType( 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 Mapping Exception无法从START_ARRAY令牌中反序列化实例全部内容,希望文章能够帮你解决Json Mapping Exception无法从START_ARRAY令牌中反序列化实例所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。