gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 19:41:35 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com