com.alibaba.fastjson.JSONObject循环给同一对象赋值会出现"$ref":"$[0]"现象问题
1.今天定义了一个JSONObject对象,引用的com.alibaba.fastjson.JSONObject,循环给这个对象赋值出现"$ref":"$[0]"现象,
/**
* fastjson中$ref对象重复引用问题
*
* 介绍:
* FastJson提供了SerializerFeature.DisableCircularReferenceDetect这个序列化选项,用来关闭引用检测。
* 关闭引用检测后,重复引用对象时就不会被$ref代替,但是在循环引用时也会导致StackOverflowError异常。
*
* 用法:
* JSON.toJSONString(object, SerializerFeature.DisableCircularReferenceDetect);
*/ 一个集合中,给相同的对象循环赋值时,它会认为是一个对象,就出现$ref,
举例:
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; public class test { public static void main(String[] args) {
JSONObject json = new JSONObject();
JSONArray array = new JSONArray();
for(int i=0;i<3;i++){
json.put("id",i);
array.add(json);
System.out.println(array);
}
System.out.println(array);
}
}
上面的例子就会出现这个现象。(但是如果把JSONObject json = new JSONObject();放到for循环之内就不会出现,因为每次循环都会新建一个对象,彼此不一样) 正确的做法之一为,关闭检测,更改之后,
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature; public class test { public static void main(String[] args) {
JSONObject json = new JSONObject();
JSONArray array = new JSONArray();
for(int i=0;i<3;i++){
json.put("id",i);
String str = JSON.toJSONString(json, SerializerFeature.DisableCircularReferenceDetect);
JSONObject jsonjson = JSON.parseObject(str);
array.add(jsonjson);
System.out.println(array);
}
System.out.println(array);
}
}
这个SerializerFeature.DisableCircularReferenceDetect就是关闭引用检测,就不会出现$ref了,
2.当然也可以吧JSONObject初始化放到for循环内,这样就不用关闭检测了。
操作网址:https://www.cnblogs.com/zj0208/p/6196632.html
自己的思路做个记录,如果侵权,请联系删除
com.alibaba.fastjson.JSONObject循环给同一对象赋值会出现"$ref":"$[0]"现象问题的更多相关文章
- 探索RequestBody报com.alibaba.fastjson.JSONObject cannot be cast to xxx
今天使用RequestBody接受前端传过来的参数,以前接受字符串数组非常成功,这次把形参改成了List<User>,原本以为顺利接受参数并映射成User的list结构,结果竟然在我取us ...
- 42-字符串到json 的错误 com.alibaba.fastjson.JSONObject cannot be cast to java.lang.String
json: {"updated_at":1551780617,"attr":{"uptime_h":3,"uptime_m&quo ...
- No message body writer has been found for class com.alibaba.fastjson.JSONObject, ContentType: */*
1:当使用 cxf 发布服务时,要求返回值类型为xml,或者json等 @Path("/searchProductByText") @GET @Produces({"ap ...
- net.sf.json.JSONOBJECT.fromObject 与 com.alibaba.fastjson.JSONObject.parseObject
文章待补充,先写写以下知识点好了. NULL值处理之 net.sf.json.JSONObject 和 com.alibaba.fastjson.JSONObject区别 JSON作为一个轻量级的文本 ...
- java后台接收json数据,报错com.alibaba.fastjson.JSONObject cannot be cast to xxx
从前台接收json封装的list数据,在后台接收时一直报错,com.alibaba.fastjson.JSONObject cannot be cast to xxx, 使用这种方式接收可以接收 @R ...
- com.alibaba.fastjson.JSONObject之对象与JSON转换方法
com.alibaba.fastjson.JSONObject时经常会用到它的转换方法,包括Java对象转成JSON串.JSON对象,JSON串转成java对象.JSON对象,JSON对象转换Java ...
- com.alibaba.fastjson.JSONObject;的使用
转: com.alibaba.fastjson.JSONObject;的使用 2018-11-04 23:51:23 mameng1998 阅读数 6404更多 分类专栏: java 1 POM ...
- Java-Class-I:com.alibaba.fastjson.JSONObject
ylbtech-Java-Class-I:com.alibaba.fastjson.JSONObject 1.返回顶部 1.1.import com.alibaba.fastjson.JSON;imp ...
- com.alibaba.fastjson.JSONObject
package com.alibaba.fastjson; import java.util.Date; import java.util.List; import com.alibaba.fastj ...
随机推荐
- Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.******.seashell.bpc.query.query.service.FscBankPayCodeQueryService
2019-03-19 16:22:14,945 WARN [main] (org.springframework.context.support.AbstractApplicationContext. ...
- Spring中applicationContext.xml详解
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- css中代码格式以及@import的语法结构
CSS中代码格式 CSS是Cascading Style Sheets(层叠样式表)的缩写.是一种对web文档添加样式的简单机制,属于表现层的布局语言. 1.基本语法规范分析一个典型CSS的语句: p ...
- C#-进制转换、基础语句、语句的总结与练习——★for循环:九九乘法表、三角形、菱形★
//for循环嵌套练习——打一个九九乘法表 ; i <= ; i++) { ; j <= i; j++) { Console.Write(j + "×" + i + & ...
- canvas+js画饼状图
效果: 源码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- JQ 文件上传
var formData = new FormData(); var name = $("input").val(); formData.append("file&quo ...
- sql自查询各种状态数据总和
- .NET中的async和await关键字使用及Task异步调用实例
其实早在.NET 4.5的时候M$就在.NET中引入了async和await关键字(VB为Async和Await)来简化异步调用的编程模式.我也早就体验过了,现在写一篇日志来记录一下顺便凑日志数量(以 ...
- Linux终端没有GUI,使用matplotlib绘图
一.解决警告信息 ... _tkinter.TclError: no display name and no $DISPLAY environment variable 两种解决方法: 1.pytho ...
- jackson构建复杂Map的序列化
1.序列化复杂的Map时,可能需要下面的方式 import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson. ...