正如本文所讨论的,我一直在尝试在Play中使用Reads[A]。
在Play Framework 2.0 Scala 中处理JSON请求
然而,当我尝试做类似的事情时,我最终得到了这个错误。
对象创建是不可能的,因为方法在trait中读取类型(json:play.api.libs.json.JsValue)models.SomeObject.AnotherObject未定义
我目前有:
package models
object SomeObject {
case class AnotherObject(val name: String)
implicit object AnotherObjectReads extends Reads[AnotherObject] {
def read(json: JsValue) =
AnotherObject((json "name").as[String])
}
}
我以以下方式使用它:
def callFunc = Action(BodyParsers.parse.json) { request =>
request.body.asOpt[SomeObject.AnotherObject].map {
//Logic
}.getOrElse(BadRequest)
}
我的代码有错吗?
我想你把它拼错了。该方法被称为读取不读取
http://www.playframework.org/documentation/api/2.0.4/scala/index.html#play.api.libs.json.Reads