com.fasterxml.jackson.databind.JsonMappingException:无法反序列化START_ARRAY令牌

时间:2015-07-08 13:26:05

标签: java json jackson

我为以下型号收到START_ARRAY错误。我期待InputDetails的对象

class InputDetails{
  public List<EachFieldDetails> fieldDetails;
}

class EachFieldDetails{
  public String fieldName;
  public String value;
}

JSON输入如下:

[{"fieldName":"siteName","value":"Warehouse"},{"fieldName":"poNumber","value":"po1"},{"fieldName":"itemCode","value":"itemcode1"},{"fieldName":"asdnSerialNo","value":"null"}]

有人可以为我提供解决方案。

这是我的班级

public Response setWHDetails(@BeanParam RequestBean requestBean,InputDetails saveInputs)
{ 
  //Do operation   
}

3 个答案:

答案 0 :(得分:2)

当您尝试反序列化为对象时,您的JSON指定了一个数组。

如果你的JSON就像:

{
    "fieldDetails" : [
        {"fieldName":"siteName","value":"Warehouse"},
        {"fieldName":"poNumber","value":"po1"},
        {"fieldName":"itemCode","value":"itemcode1"},
        {"fieldName":"asdnSerialNo","value":"null"}
    ]
}

可能工作。或者,您可以直接反序列化为数组。

我说可能是,因为您没有提供任何代码或信息,说明您正在使用哪个工具或库来处理反序列化过程。

答案 1 :(得分:1)

你可能需要这个:

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

public class JacksonRead {
    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

[{"fieldName":"siteName","value":"Warehouse"},{"fieldName":"poNumber","value":"po1"},{"fieldName":"itemCode","value":"itemcode1"},{"fieldName":"asdnSerialNo","value":"null"}]

<强> EachFieldDetails.java

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.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
                + "]";
    }
}

答案 2 :(得分:0)

使用您的模型和以下代码我没有发现任何错误:

    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);