加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Jackson中的ObjectMapper序列化

发布时间:2020-12-14 16:21:01 所属栏目:Java 来源:网络整理
导读:我想通过使用对象映射器来序列化不同类型的列表,但是我不知道如何 一次将不同类型的列表对象传递给对象Mapper.以下是我的代码: AccountingService accService = ServiceFactory.getAccountingService();ListTaxCategory taxCategoryList = accService.getAl
我想通过使用对象映射器来序列化不同类型的列表,但是我不知道如何
一次将不同类型的列表对象传递给对象Mapper.以下是我的代码:
AccountingService accService      = ServiceFactory.getAccountingService();
List<TaxCategory> taxCategoryList = accService.getAllTaxCategories();
ProductService productService     = ServiceFactory.getProductService();
List<SimpleUom> simpleUomList     = productService.getSimpleUomsList();

ObjectMapper objMapper;
objMapper.writeValueAsString(?)--

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

解决方法

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

根据评论编辑:

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

public class MyLists {
    private List<TaxCategory> taxCategoryList;
    private List<SimpleUom> simpleUomList;
    // + constructor,getters and setters
}

ObjectMapper objMapper = new ObjectMapper();
MyLists myLists = new MyLists(taxCategoryList,simpleUomList);
String jsonString = objMapper.writeValueAsString(myLists);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读