Jackson JSON + Java 泛型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4446514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 17:36:04  来源:igfitidea点击:

Hymanson JSON + Java Generics

javajsongenericsHymansondeserialization

提问by Ravi

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

我正在尝试List<Bill使用Hymanson json 库将以下 JSON 反序列化/映射到> java 对象。(这个 json 是由 Hymanson 生成的,为简洁起见,我省略了那部分)

{"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.Hymanson.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.Hymanson.map.JsonMappingException.from(JsonMappingException.java:160)
 at org.codehaus.Hymanson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:194)
 at org.codehaus.Hymanson.map.deser.CollectionDeserializer.deserialize(CollectionDeserializer.java:103)
 at org.codehaus.Hymanson.map.deser.CollectionDeserializer.deserialize(CollectionDeserializer.java:93)
 at org.codehaus.Hymanson.map.deser.CollectionDeserializer.deserialize(CollectionDeserializer.java:25)
 at org.codehaus.Hymanson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:1980)
 at org.codehaus.Hymanson.map.ObjectMapper.readValue(ObjectMapper.java:1278)

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

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

@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?

我想我错过了一些明显的东西..但不确定它是什么..有什么想法吗?

采纳答案by Jason Nichols

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:

问题不在于您的代码,而在于您的示例输入。您实际尝试反序列化的是一个带有名为“bills”的字段的对象,而不是一个列表!您应该用作输入的是:

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

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

这是一个对象数组,它被转换为一个列表。

回答by neoreeprast

Try using ObjectWriter instead of ObjectMapper

尝试使用 ObjectWriter 而不是 ObjectMapper

Writer writer=new StringWriter();

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

I'm using Hymanson 1.9.2

我正在使用Hyman逊 1.9.2