杰克逊JSON + Java泛型 [英] Jackson JSON + Java Generics

查看:95
本文介绍了杰克逊JSON + Java泛型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用 Jackson json库反序列化/映射下面的JSON到 List< Bill > java对象。 (这个json是由jackson生成的,Iam为了简洁省略了这个部分)

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

这是我的转换方法: / p>

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

我得到这个错误:

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

这是我简化的spring3控制器,它返回i / p json(将Jackson映射配置为默认视图):

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?

推荐答案

问题不在于您的代码,而在于您的示例输入。你实际上试图反序列化的是一个名为bills的字段,而不是一个列表!你应该使用什么作为输入:

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.

这篇关于杰克逊JSON + Java泛型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆