Jackson中的ObjectMapper序列化

前端之家收集整理的这篇文章主要介绍了Jackson中的ObjectMapper序列化前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想通过使用对象映射器来序列化不同类型的列表,但是我不知道如何
一次将不同类型的列表对象传递给对象Mapper.以下是我的代码
  1. AccountingService accService = ServiceFactory.getAccountingService();
  2. List<TaxCategory> taxCategoryList = accService.getAllTaxCategories();
  3. ProductService productService = ServiceFactory.getProductService();
  4. List<SimpleUom> simpleUomList = productService.getSimpleUomsList();
  5.  
  6. ObjectMapper objMapper;
  7. objMapper.writeValueAsString(?)--

你能否建议我必须通过什么?在上面的代码.这是因为我必须将包含上述列表的jackson序列化字符串作为jsp中的单个字符串,并解析该字符串以获取在客户端使用的单个列表.

解决方法

只要试试:
  1. ObjectMapper objMapper = new ObjectMapper();
  2. String jsonString = objMapper.writeValueAsString(simpleUomList);

根据评论编辑:

您需要创建一个包装两个列表的类,然后写入它:

  1. public class MyLists {
  2. private List<TaxCategory> taxCategoryList;
  3. private List<SimpleUom> simpleUomList;
  4. // + constructor,getters and setters
  5. }
  6.  
  7. ObjectMapper objMapper = new ObjectMapper();
  8. MyLists myLists = new MyLists(taxCategoryList,simpleUomList);
  9. String jsonString = objMapper.writeValueAsString(myLists);

猜你在找的Java相关文章