Question

I am trying to deserialize/map the below JSON to List<Bill> java object using Jackson json library. (this json was generated by jackson, Iam omitting that piece for brevity)

{"bills":[{"amount":"13","billId":"billid3"}]}

Here is my conversion method:

private static void convert(){
   String jsonBill =  "{\"bills\":[{\"amount\":\"13\",\"billId\":\"billid3\"}]}";

   ObjectMapper mapper = new ObjectMapper();
   List<Bill> bills = null;
   try {
       bills = mapper.readValue(jsonBill, new TypeReference<List<Bill>>() { });
   } catch (Exception e) {
       e.printStackTrace();
   }
   System.out.println("bills = " + bills.size());
}

The Bill entity is below:

@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS)
public class Bill { 
   private String amount;
   private String billId;

   public String getBillId() {
       return billId;
   }
   public void setBillId(String billId) {
       this.billId = billId;
   }
   public String getAmount() {
       return amount;
   }
   public void setAmount(String amount) {
       this.amount = amount;
   } 
}

and I get this error:

**org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.List out of START_OBJECT token
 at [Source: java.io.StringReader@7a84e4; line: 1, column: 1]**
 at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:160)
 at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:194)
 at org.codehaus.jackson.map.deser.CollectionDeserializer.deserialize(CollectionDeserializer.java:103)
 at org.codehaus.jackson.map.deser.CollectionDeserializer.deserialize(CollectionDeserializer.java:93)
 at org.codehaus.jackson.map.deser.CollectionDeserializer.deserialize(CollectionDeserializer.java:25)
 at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:1980)
 at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1278)

Here is my simplified spring3 controller which returns the i/p json (with Jackson mapping configured as default view):

@ModelAttribute("bills")
@RequestMapping(value = "/", method = RequestMethod.GET)
public List<Bill> fetchBills() throws IOException {
    Bill bill = new Bill();
    bill.setAmount("13");
    bill.setBillId("billid3");

    List<Bill> bills = new ArrayList<Bill>();
    bills.add(bill);
    return bills;
}

I guess I am missing something obvious.. but not sure what it is.. Any ideas?

Was it helpful?

Solution

The problem lies not in your code, but your example input. What you're actually trying to deserialize is an object with a field named "bills", not a list! What you should be using as input is:

[{"billId":"billid3","amount":"13"}]

This is an array of objects, which is converted to a list.

OTHER TIPS

Try using ObjectWriter instead of ObjectMapper

Writer writer=new StringWriter();

        ObjectWriter oWriter=om.writerWithType(new TypeReference<List<Bill>>() {
        });
        oWriter.writeValue(writer, result);

I'm using jackson 1.9.2

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top