Json.fromJson不适用于Int或Double

时间:2017-03-12 14:28:19

标签: json playframework

如何解决以下隐式冲突?我使用Json.toJson将Double转换为JsValue。反向过程无效。存在隐含的冲突。

scala> val doublejsv = Json.toJson(4.1)
doublejsv: play.api.libs.json.JsValue = 4.1

scala> Json.fromJson(doublejsv)
<console>:21: error: ambiguous implicit values:
 both object IntReads in trait DefaultReads of type play.api.libs.json.Reads.IntReads.type
 and object ShortReads in trait DefaultReads of type play.api.libs.json.Reads.ShortReads.type
 match expected type play.api.libs.json.Reads[T]
       Json.fromJson(doublejsv)
                    ^

1 个答案:

答案 0 :(得分:1)

使用显式类型:

val double = Json.fromJson[Double](doublejsv)