Scala中fastjson的toJSONString方法使用踩坑

在使用JSON.toJSONString将对象转化为json字符串是,使用如下操作:

    import com.alibaba.fastjson.JSON

    val accompany_rank = rankQueryRemoteService.queryRankList(request)
    JSON.toJSONString(accompany_rank)

结果运行是报错了

Error:(26, 10) ambiguous reference to overloaded definition,
both method toJSONString in class JSON of type (x$1: Any, x$2: com.alibaba.fastjson.serializer.SerializerFeature*)String
and  method toJSONString in class JSON of type (x$1: Any)String
match argument types (com.yupaopao.platform.common.dto.Response[com.yupaopao.platform.common.dto.PageResult[com.yupaopao.platform.rank.api.response.TopsRankDTO]]) and expected result type String
    JSON.toJSONString(accompany_rank)

出于某种原因,Scala重载逻辑与Java逻辑不匹配。需要使用:

JSON.toJSONString(map, SerializerFeature.PrettyFormat)

将原有代码修改为

    import com.alibaba.fastjson.JSON

    val accompany_rank = rankQueryRemoteService.queryRankList(request)
    JSON.toJSONString(accompany_rank, SerializerFeature.PrettyFormat)

就可以了

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容