Java Json 映射异常无法从 START_ARRAY 令牌反序列化实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27154631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Json Mapping Exception can not deserialize instance out of START_ARRAY token
提问by Marcin Erbel
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:
我正在尝试将我的 json 请求解析到我的模型。我不知道这段代码有什么问题。json 的语法看起来正确,Java 模型上的注释也正确。我不知道为什么我会收到如下错误:
Caused by: org.codehaus.Hymanson.map.JsonMappingException: Can not deserialize instance of ParametersType out of START_ARRAY token
(through reference chain: Document["parameters"])
Java model:
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 code:
json代码:
{
"templateId": "123",
"parameters": [
{
"parameter": [
{
"key": "id",
"value": "1",
"type": "STRING_TYPE"
},
{
"key": "id2",
"value": "12",
"type": "STRING_TYPE"
}
]
}
],
"documentFormat": "PDF"
}
采纳答案by gregwhitaker
You have declared parameters
as a single object, but you are returning it as an array of multiple objects in your JSON document.
您已声明parameters
为单个对象,但您将其作为 JSON 文档中多个对象的数组返回。
Your model currently defines the parameters node as a ParametersType
object:
您的模型当前将参数节点定义为ParametersType
对象:
@JsonProperty( "parameters" )
@XmlElement( required = true )
protected ParametersType parameters;
This means your model object is expecting a JSON document that looks like the following:
这意味着您的模型对象需要一个如下所示的 JSON 文档:
{
"templateId": "123",
"parameters": {
"parameter": [
{
"key": "id",
"value": "1",
"type": "STRING_TYPE"
},
{
"key": "id2",
"value": "12",
"type": "STRING_TYPE"
}
]
},
"documentFormat": "PDF"
}
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:
但是在您的 JSON 文档中,您将返回一个ParametersType
对象数组。因此,您需要将模型更改为 ParametersType 对象列表:
@JsonProperty( "parameters" )
@XmlElement( required = true )
protected List<ParametersType> parameters;
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.
您正在返回一个 ParametersType 对象数组的事实是解析器抱怨无法从 START_ARRAY 反序列化对象的原因。它正在寻找具有单个对象的节点,但在您的 JSON 中找到了一组对象。