【发布时间】:2013-01-11 03:49:30
【问题描述】:
我有一个 Scala 案例类
case class Example(name: String, number: Int)
和一个伴随对象
object Example {
implicit object ExampleFormat extends Format[Example] {
def reads(json: JsValue) = {
JsSuccess(Example(
(json \ "name").as[String],
(json \ "number").as[Int]))
}
def writes(...){}
}
}
将 JSON 转换为 Scala 对象。
当 JSON 有效时(即 {"name":"name","number": 0} 它可以正常工作。但是,当 number 在引号中时 {"name":"name","number":"0"} 我得到一个错误:validate.error.expected.jsnumber。
在这种情况下有没有办法将String隐式转换为Int(假设数字有效)?
【问题讨论】:
标签: scala playframework-2.0 playframework-2.1