Fastjson BeanToArray
Dalam fastjson, mod pemetaan yang dipanggil BeanToArray disokong. Dalam mod biasa, JavaBean dipetakan ke objek json, dan mod BeanToArray dipetakan kepada tatasusunan json.
Contoh 1
class Mode { public int id; public int name; } Model model = new Model(); model.id = 1001; model.name = "gaotie"; // {"id":1001,"name":"gaotie"} String text_normal = JSON.toJSONString(model); // [1001,"gaotie"] String text_beanToArray = JSON.toJSONString(model, SerializerFeature.BeanToArray); // support beanToArray & normal mode JSON.parseObject(text_beanToArray, Feature.SupportArrayToBean);
Dalam contoh di atas, dalam mod BeanToArray, tiada output Key, yang menjimatkan ruang, rentetan json lebih kecil dan prestasi akan menjadi lebih baik. . kebolehbacaan yang lebih baik. Contoh 3
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);