1.list中放入同一个对象,会出现内存地址引用{"$ref":"#[0]"},后台可以识别,但是前台不会识别
@Test public void testList(){ User user = new User(); user.setUserName("cgx"); user.setPassword("123456"); User user1 = new User(); user1.setUserName("zbz"); user1.setPassword("123456"); ArrayList list = new ArrayList(); list.add(user); list.add(user1); list.add(user); String jsonString = JSON.toJSONString(list,SerializerFeature.DisableCircularReferenceDetect);//关闭循环引用 System.out.println(jsonString); }
JSON.toJSONString(list,true),会格式化json,方便查看
2.对象相互包含
@Test public void testList1(){ User user = new User(); user.setUserName("cgx"); user.setPassword("123456"); HashSet<Role> roles = new HashSet<Role>(); HashSet<User> users = new HashSet<User>(); users.add(user); Role role = new Role(); role.setName("测试角色"); role.setUsers(users); roles.add(role); user.setRoles(roles); //两种办法防止循环引用 //1.在bean的属性上直接@JSONField(serialize=false)去掉不需要转换的属性 //2. // 遇到sid和sname属性正常进行转换json SimplePropertyPreFilter filter = new SimplePropertyPreFilter("password","roles","name"); String jsonString = JSON.toJSONString(user,filter,SerializerFeature.DisableCircularReferenceDetect); System.out.println(jsonString); }
在实体类中使用@JSONField(serialize=false),是不是此字段就不返回了,放弃转换该字段
filter进行过滤,想要现实的字段,进行转换的字段,写在过滤中,其他不进行转换