Fastjson之BeanToArray的功能简介说明
下文笔者讲述Bean转换为数组的方法及示例简介说明,如下所示
BeanToArray: 指将bean映射为json array数组例1
class Mode { public int id; public int name; } Model model = new Model(); model.id = 1001; model.name = "java265"; // {"id":1001,"name":"java265"} String text_normal = JSON.toJSONString(model); // [1001,"java265"] String text_beanToArray = JSON.toJSONString(model, SerializerFeature.BeanToArray); // support beanToArray & normal mode JSON.parseObject(text_beanToArray, Feature.SupportArrayToBean);例2
class Company { public int code; public list<Department> departments = new ArrayList<Department>(); } @JSONType(serialzeFeatures=SerializerFeature.BeanToArray, parseFeatures=Feature.SupportArrayToBean) class Department { public int id; public Stirng name; public Department() {} public Department(int id, String name) {this.id = id; this.name = name;} } Company company = new Company(); company.code = 100; company.departments.add(new Department(1001, "Sales")); company.departments.add(new Department(1002, "Financial")); // {"code":10,"departments":[[1001,"Sales"],[1002,"Financial"]]} String text = JSON.toJSONString(commpany);例3
class Company { public int code; @JSONField(serialzeFeatures=SerializerFeature.BeanToArray, parseFeatures=Feature.SupportArrayToBean) public List<Department> departments = new ArrayList<Department>(); }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。