Sachin S Rao
Sachin S Rao

Reputation: 139

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize out of START_ARRAY token

I'm getting START_ARRAY error for following model. I'm expecting object of InputDetails

class InputDetails{
  public List<EachFieldDetails> fieldDetails;
}

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

JSON input is as follows:

[{"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   
}

Upvotes: 3

Views: 23223

Answers (3)

Arpit Aggarwal
Arpit Aggarwal

Reputation: 29316

You might need this:

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
                + "]";
    }
}

Upvotes: 1

tom
tom

Reputation: 729

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

Upvotes: 0

Baldy
Baldy

Reputation: 2002

Your JSON specifies an array while you're trying to deserialize into an object.

if your JSON was like:

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

It would probably work. Alternately, you could deserialize directly into an array.

I say probably because you haven't provided any code or information of what tool or library you're using to handle the deserialization process.

Upvotes: 2

Related Questions