1
var bom = []; var Basket = []; fruitObj{}; fruitObj.fruit = "Apple"; fruitObj.quantity = "1"; Basket.push[fruitObj] fruitObj.fruit = "Grape"; fruitObj.quantity = "10"; Basket.push[fruitObj] 

JSON(bom.push[Basket]) -> received at server [[{\"fruit\":\"Apple\",\"quantity\":\"1\"},{\"fruit\":\"Grape\",\"quantity\":\"10\"}]]

Fruit.java

@JsonIgnoreProperties(ignoreUnknow = true) class Fruit(){ String fruit; String quantity; String price; //getters //setters } 

At Controller

List<List<?>> list = mapper.readValue(bom,TypeFactory.defaultInstance().constructCollectionType(List.class, FruitList.class)); 

FruitList.java

FruitList() { List<List<Fruit>> fruits; //getters (List<List<Fruit>>) //setters } 

I am facing org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance out of START_ARRAY token.

I have tried various combinations of List<>, List< List<> >, List< MyObjList >, MyObjList< Fruit > etc but unable to exactly parse it, however I am able to parse with mapper as List by passing Fruit.class for the below string [{\"fruit\":\"Apple\",\"quantity\":\"1\"},{\"fruit\":\"Grape\",\"quantity\":\"10\"}]

1
  • JavaScript may be required if it is required to structure the array in some way to meet my requirements considering parsing from arrays to list of objects. If you down vote it even before ther is a response it gives unnecessary signals to the community to avoid it, I have been trying since few hours trying to parse it into various array dimensions in associative arrays and otherwise as well as converting at server into lists without success Commented Dec 22, 2017 at 13:52

1 Answer 1

1

Try this:

String json = "[[{\"fruit\":\"Apple\",\"quantity\":\"1\"},{\"fruit\":\"Grape\",\"quantity\":\"10\"}]]"; List<List<Fruit>> fruits = new ObjectMapper().readValue(json, new TypeReference<List<List<Fruit>>>() {}); 

Of course, consider reusing ObjectMapper instance in order not to create it every time.

A faster solution would be with using array of arrays, as follows:

Fruit[][] fruits = new ObjectMapper().readValue(json, Fruit[][].class); 

Though you may find it less convenient.

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

1 Comment

thank you for that response, I am new here and not sure why it is showing 0 points, however I shall mark it answered by tomorrow and the answer looks neat

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.