解析json对象出现$ref: "$.list[0]"的解决办法
http://blog.csdn.net/u013185616/article/details/52799166 [{"endsyntime":"2017-11-15 16:31:29","id":2,"intervaltime":"指定日期执行一次","sourcecount":15,"sourcedbid":6,"sourcetable":"test2","startsyntime":"2017-11-15 16:44:52","starttime":"2017-11-15 10:10:10","targettable":"test2"},{"$ref":"$[0]"},{"$ref":"$[0]"}] 我的原因是;添加数据时,new对象时,在循环外new的,list添加数据的时候,每次都add这一个obj. JsonSynModel synm = new JsonSynModel(); //错误的原由,循环添加同一个obj for (synmodel s : list) { synm = new JsonSynModel(); synm.setId(s.getId()); synm.setWeek(week); synm.setComment(s.getComment()); reList.add(synm); } return JSON.toJSONString(reList); 原因 : (多表查询,死循环) 循环引用:当一个对象包含另一个对象时,fastjson就会把该对象解析成引用。引用是通过$ref标示的,下面介绍一些引用的描述 "$ref":".." 上一级 "$ref":"@" 当前对象,也就是自引用 "$ref":"$" 根对象 "$ref":"$.children.0" 基于路径的引用,相当于 root.getChildren().get(0) (解除引用;ssh一对多,多对多维护一端关系,防止多表关联查询出现死循环) 解决办法 : 采用禁止循环引用的方案. return JSON.toJSONString(reList,SerializerFeature.DisableCircularReferenceDetect); 其中:SerializerFeature.DisableCircularReferenceDetect就是禁止循环引用的方案,我们可以通过枚举类SerializerFeature来查看到底有多少种方式: public enum SerializerFeature { QuoteFieldNames, UseSingleQuotes, WriteMapNullValue, WriteEnumUsingToString, UseISO8601DateFormat, /** * @since 1.1 */ WriteNullListAsEmpty, /** * @since 1.1 */ WriteNullStringAsEmpty, /** * @since 1.1 */ WriteNullNumberAsZero, /** * @since 1.1 */ WriteNullBooleanAsFalse, /** * @since 1.1 */ SkipTransientField, /** * @since 1.1 */ SortField, /** * @since 1.1.1 */ @Deprecated WriteTabAsSpecial, /** * @since 1.1.2 */ PrettyFormat, /** * @since 1.1.2 */ WriteClassName, /** * @since 1.1.6 */ DisableCircularReferenceDetect, /** * @since 1.1.9 */ WriteSlashAsSpecial, /** * @since 1.1.10 */ BrowserCompatible, /** * @since 1.1.14 */ WriteDateUseDateFormat, /** * @since 1.1.15 */ NotWriteRootClassName, /** * @since 1.1.19 */ DisableCheckSpecialChar, /** * @since 1.1.35 */ BeanToArray ; private SerializerFeature(){ mask = (1 << ordinal()); } private final int mask; public final int getMask() { return mask; } public static boolean isEnabled(int features, SerializerFeature feature) { return (features & feature.getMask()) != 0; } public static int config(int features, SerializerFeature feature, boolean state) { if (state) { features |= feature.getMask(); } else { features &= ~feature.getMask(); } return features; } }