Jackson serialization with ObjectMapper in java

前端 未结 1 1907
悲&欢浪女
悲&欢浪女 2021-01-08 00:00

I want to serialize the different types of lists by using object mapper, but I do not know how to pass the different types of list objects into object Mapper at a time. T

相关标签:
1条回答
  • 2021-01-08 00:21

    Simply try:

    ObjectMapper objMapper = new ObjectMapper();
    String jsonString = objMapper.writeValueAsString(simpleUomList);
    

    Edit according to the comment:

    You need to create a class wrapping your two lists and then write it:

    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);
    
    0 讨论(0)
提交回复
热议问题