简体   繁体   English

如何使用 Jackson 将对象序列化为 json,包括 ArrayList

[英]How to serialize object to json with Jackson, including ArrayList

I have this simple java object我有这个简单的 java 对象

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.我想使用 Jackson 将其序列化为 JSON。 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 ?如何序列化包含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. Jackson 依赖公共访问器来确定要序列化的字段。

Your list is the only field that isn't publicly exposed, so that's why it's hidden in the output您的列表是唯一未公开的字段,这就是它隐藏在输出中的原因

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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