序列化Record list出现columnNames,columnValues,columns

使用json序列化 List<Record>出现问题

微信截图_20181130175644.png

结果返回

{"code":"","message":"","result":[{"columnNames":["name","value"],"columnValues":[0,0],"columns":{"name":0,"value":0}},{"columnNames":["name","value"],"columnValues":[1,1],"columns":{"name":1,"value":1}},{"columnNames":["name","value"],"columnValues":[2,2],"columns":{"name":2,"value":2}},{"columnNames":["name","value"],"columnValues":[3,3],"columns":{"name":3,"value":3}},{"columnNames":["name","value"],"columnValues":[4,4],"columns":{"name":4,"value":4}},{"columnNames":["name","value"],"columnValues":[5,5],"columns":{"name":5,"value":5}},{"columnNames":["name","value"],"columnValues":[6,6],"columns":{"name":6,"value":6}},{"columnNames":["name","value"],"columnValues":[7,7],"columns":{"name":7,"value":7}},{"columnNames":["name","value"],"columnValues":[8,8],"columns":{"name":8,"value":8}},{"columnNames":["name","value"],"columnValues":[9,9],"columns":{"name":9,"value":9}}],"success":true}

评论区

zhc

2018-11-30 18:01

fastjson 使用的是1.2.47版本

JFinal

2018-11-30 18:16

既然是用的 jfinal, 得这么来用:
FastJson.getJson().toJson(...)

因为 record 没有 setter 方法,而 fastjson 默认是根据 getter 方法来转换的,所以 jfinal 内部对 fastjson 做了扩展,详情看一下 com.jfinal.json 包下面的源码

jfinal 文档频道也对 json 的机制与配置做了详细说明,看一次能省很多时间

zhc

2018-12-01 13:30

dwewqd

2019-12-27 11:39

@JFinal 你好 我看了FastJson.getJson().toJson(...)源码 有点不明白和直接调用JSON.toJSONString的区别在哪。。求指教

JFinal

2019-12-27 11:56

@dwewqd 基本上是一样的,前者是 jfinal 对 fastjson 的封装,所以里面添加了对 Record 的转换处理,而 JSON.toJSONString(...) 不支持对 Record 对象的转换

dwewqd

2019-12-27 12:10

@JFinal 你提到的“添加了对 Record 的转换处理”体现在哪? 有点没看懂 我新手

JFinal

2019-12-27 17:44

@dwewqd 对 Record 的支持代码这里:
https://gitee.com/jfinal/jfinal/blob/master/src/main/java/com/jfinal/json/FastJsonRecordSerializer.java

dwewqd

2019-12-30 11:40

@JFinal 谢谢指导!

热门反馈

扫码入社