gpt4 book ai didi

json - 是否可以使Scala的JSON.parseFull()不将整数视为小数?

转载 作者:行者123 更新时间:2023-12-04 21:55:18 24 4
gpt4 key购买 nike

我正在使用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

scala> println(JSON.parseFull(jStr))

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

关于json - 是否可以使Scala的JSON.parseFull()不将整数视为小数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18945949/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com