加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

是否有可能使Scala的JSON.parseFull()不将整数视为小数?

发布时间:2020-12-16 09:27:23 所属栏目:安全 来源:网络整理
导读:我正在使用JSON.parseFull()解析此字符串.这个方法对我来说非常方便,因为我需要一张Map val jStr = """{"wt":"json","rows":500}"""println( JSON.parseFull(jStr) ) 这是输出: Some(Map(wt - json,rows - 500.0)) // ′rows′ as Double 我想取回一个Integ
我正在使用JSON.parseFull()解析此字符串.这个方法对我来说非常方便,因为我需要一张Map

val jStr = """{"wt":"json","rows":500}"""
println( JSON.parseFull(jStr) )

这是输出:

Some(Map(wt -> json,rows -> 500.0)) // ′rows′ as Double

我想取回一个Integer而不是Double.

Some(Map(wt -> json,rows -> 500)) // ′rows′ as Integer

那可能吗?

解决方法

从 Scala’s JSON documentation起

The default conversion for numerics is into a double. If you wish to override this behavior at the global level,you can set the globalNumberParser property to your own (String => Any) function. If you only want to override at the per-thread level then you can set the perThreadNumberParser property to your function

在你的情况下:

val myConversionFunc = {input : String => Integer.parseInt(input)}
JSON.globalNumberParser = myConversionFunc

阶> println(JSON.parseFull(jStr))

一些(地图(wt – > json,行 – > 500))

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读