java com.fasterxml.jackson.databind.JsonMappingException:无法反序列化 START_ARRAY 令牌
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31294121/
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
com.fasterxml.Hymanson.databind.JsonMappingException: Can not deserialize out of START_ARRAY token
提问by Sachin S Rao
I'm getting START_ARRAY error for following model. I'm expecting object of InputDetails
我收到以下模型的 START_ARRAY 错误。我期待 InputDetails 的对象
class InputDetails{
public List<EachFieldDetails> fieldDetails;
}
class EachFieldDetails{
public String fieldName;
public String value;
}
JSON input is as follows:
JSON输入如下:
[{"fieldName":"siteName","value":"Warehouse"},{"fieldName":"poNumber","value":"po1"},{"fieldName":"itemCode","value":"itemcode1"},{"fieldName":"asdnSerialNo","value":"null"}]
Can someone provide me the solution.
有人可以为我提供解决方案。
Here is my class
这是我的课
public Response setWHDetails(@BeanParam RequestBean requestBean,InputDetails saveInputs)
{
//Do operation
}
采纳答案by Baldy
Your JSON specifies an array while you're trying to deserialize into an object.
当您尝试反序列化为对象时,您的 JSON 指定了一个数组。
if your JSON was like:
如果你的 JSON 是这样的:
{
"fieldDetails" : [
{"fieldName":"siteName","value":"Warehouse"},
{"fieldName":"poNumber","value":"po1"},
{"fieldName":"itemCode","value":"itemcode1"},
{"fieldName":"asdnSerialNo","value":"null"}
]
}
It would probablywork. Alternately, you could deserialize directly into an array.
它可能会起作用。或者,您可以直接反序列化为数组。
I say probablybecause you haven't provided any code or information of what tool or library you're using to handle the deserialization process.
我这么说可能是因为你没有提供任何关于你用来处理反序列化过程的工具或库的代码或信息。
回答by Arpit Aggarwal
You might need this:
你可能需要这个:
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.codehaus.Hymanson.map.ObjectMapper;
import org.codehaus.Hymanson.type.TypeReference;
public class HymansonRead {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
try {
TypeReference<List<EachFieldDetails>> typeRef = new TypeReference<List<EachFieldDetails>>() {
};
List<EachFieldDetails> user = mapper.readValue(new File(
"d:\user.json"), typeRef);
System.out.println(user);
} catch (IOException e) {
e.printStackTrace();
}
}
}
user.json
用户.json
[{"fieldName":"siteName","value":"Warehouse"},{"fieldName":"poNumber","value":"po1"},{"fieldName":"itemCode","value":"itemcode1"},{"fieldName":"asdnSerialNo","value":"null"}]
EachFieldDetails.java
每个字段详细信息.java
import com.fasterxml.Hymanson.annotation.JsonInclude;
import com.fasterxml.Hymanson.annotation.JsonProperty;
import com.fasterxml.Hymanson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({ "fieldName", "value" })
public class EachFieldDetails {
@JsonProperty("fieldName")
private String fieldName;
@JsonProperty("value")
private String value;
/**
*
* @return The fieldName
*/
public String getFieldName() {
return fieldName;
}
/**
*
* @param fieldName
* The fieldName
*/
public void setFieldName(String fieldName) {
this.fieldName = fieldName;
}
/**
*
* @return The value
*/
public String getValue() {
return value;
}
/**
*
* @param value
* The value
*/
public void setValue(String value) {
this.value = value;
}
@Override
public String toString() {
return "EachFieldDetail [fieldName=" + fieldName + ", value=" + value
+ "]";
}
}
回答by tom
I have not found any error by using your model and below code:
通过使用您的模型和以下代码,我没有发现任何错误:
String str="[{\"fieldName\":\"siteName\",\"value\":\"Warehouse\"},{\"fieldName\":\"poNumber\",\"value\":\"po1\"},{\"fieldName\":\"itemCode\",\"value\":\"itemcode1\"},{\"fieldName\":\"asdnSerialNo\",\"value\":\"null\"}]";
ObjectMapper map=new ObjectMapper();
JsonNode node=map.readTree(str);