公司使用jackson进行json串的生成,现在碰上一个问题,hibernate查询出来的数据是有可能出现循环依赖的,使用jackson进行生成json str时候,报循环依赖错误.
解决办法有两个
1,使用jackson中的注解@JsonIgnore 解释一下 @JsonIgnore 这个的作用相当于解析器遇上这个注解的时候,会进行自动忽略这个属性,也就是中断了循环
这样能解决问题,但是会导致数据丢失
2.使用阿里的fastjson来解决这个问题
package com.hit.fastjson;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.junit.Test;
/**
* Created by zh on 2016/10/8.
*/
public class TestJson {
@Test
public void testJson2(){
People a = new People();
People b = new People();
a.setName("a");
b.setName("b");
a.setParent(b);
b.setParent(a);
String str = JSON.toJSONString(b);
System.out.println(str);
People b2 = JSON.parseObject(str, People.class);
System.out.printf(b2.getParent().getName());
}
}
附上阿里 fastjson的github地址
https://github.com/alibaba/fastjson/wiki/Samples-DataBind