简体   繁体   中英

How to serialize object to json with Jackson, including ArrayList

I have this simple java object

public class Order {
    static public class Product {
        public String name;
        public Integer quantity;
        public Float price;
    }
    public String clientName;
    public String clientPhone;
    ArrayList<Product> products = new ArrayList<Product>();
    public Float total;
}

I want to serialize it to JSON using Jackson. I do like this:

    String _json;
    ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
    _json = ow.writeValueAsString(order);

but it won't serialie the array list in my order, only the other members.

How do I serialize an object that contains an ArrayList ? If it's not possible is there another container class that I can use which is easy to serialize?

Jackson depends on public accessors to determine fields to serialize.

Your list is the only field that isn't publicly exposed, so that's why it's hidden in the output

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM