java - Json 映射异常无法从 START_ARRAY token 中反序列化实例

标签 java json jackson cxf jax-rs

我正在尝试将我的 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;

....}

Json代码:

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

最佳答案

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

您的模型当前将参数节点定义为 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 中找到了一个对象数组。

关于java - Json 映射异常无法从 START_ARRAY token 中反序列化实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27154631/

相关文章:

java - 如何优化android代码

javascript - 如何在 d3.js (albersUsa) 中为每个状态添加标签?

java - 从android中的Json String中删除反斜杠

java - 用于第 3 方库的自定义 jackson 反序列化器

java - 使用 Spring MVC 如何为 java 8 LocalDateTime 添加自定义 JsonSerializer?

java - 无法解析符号 'servlet'

java - 如何在Spring Boot中默认阻止@PathVariable值的解码

java - Jackson - 由于构造函数导致的 JsonMappingException

java - 在 C 中使用 JNI 从对象获取对象

c# - Json.net 反序列化具有非字符串键类型的嵌套字典