是否可以使Scala的JSON.parseFull()不将整数视为小数? [英] Is it possible to make Scala's JSON.parseFull() not to treat Integers as Decimals?

查看:513
本文介绍了是否可以使Scala的JSON.parseFull()不将整数视为小数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JSON.parseFull()解析此字符串.这种方法对我来说真的很方便,因为我需要获取地图

I'm parsing this string using JSON.parseFull(). This method is really convenient to me because I need to get a Map

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

这是输出:

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

我想找回整数而不是双精度数.

I'd like to get back an Integer instead of a Double.

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

有可能吗?

推荐答案

来自

数字的默认转换为双精度.如果希望在全局级别覆盖此行为,可以将globalNumberParser属性设置为自己的(String => Any)函数.如果您只想在每个线程级别进行覆盖,则可以将perThreadNumberParser属性设置为您的函数

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

scala> println(JSON.parseFull(jStr))

scala> println( JSON.parseFull(jStr) )

Some(Map(wt-> json,行-> 500))

Some(Map(wt -> json, rows -> 500))

这篇关于是否可以使Scala的JSON.parseFull()不将整数视为小数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆