3

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 } 
2
  • Could u plz share ur code? Commented Jul 8, 2015 at 13:30
  • 1
    show us the code that does the parsing please Commented Jul 8, 2015 at 13:30

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I forgot to include the variable name.Thanks a lot.
1

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

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.