Objectmapper serialization in Jackson
•
Java
I want to serialize different types of lists by using an object mapper, but I don't know how
AccountingService accService = ServiceFactory.getAccountingService(); List<TaxCategory> taxCategoryList = accService.getAllTaxCategories(); ProductService productService = ServiceFactory.getProductService(); List<SimpleUom> simpleUomList = productService.getSimpleUomsList(); ObjectMapper objMapper; objMapper.writeValueAsString(?)--
Can you suggest what I have to pass? The code above This is because I have to take the Jackson serialized string containing the above list as a single string in the JSP and parse the string to get a single list used on the client
Solution
Just try:
ObjectMapper objMapper = new ObjectMapper(); String jsonString = objMapper.writeValueAsString(simpleUomList);
Edit according to comments:
You need to create a class that wraps two lists and 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);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码