美文网首页
Scala中fastjson的toJSONString方法使用踩

Scala中fastjson的toJSONString方法使用踩

作者: 陈凌川 | 来源:发表于2021-07-16 15:18 被阅读0次

在使用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)

就可以了

相关文章

网友评论

      本文标题:Scala中fastjson的toJSONString方法使用踩

      本文链接:https://www.haomeiwen.com/subject/cthbpltx.html