以下代码给出了
的编译错误No implicit Reads for controllers.Inner available.
如果我改变
case class Outer (in : Inner)
到
case class Outer (in : String)
它编译得很好,我可以解析Outer
和Inner
json。
使用嵌入对象解析JSON的最佳方法是什么?
case class Outer (in : Inner)
object Outer {
implicit val reads = Json.reads[Outer]
}
case class Inner (deep: String)
object Inner {
implicit val reads = Json.reads[Inner]
}
object Test extends Controller {
def tester = Action {
implicit request =>
val json = request.body.asJson.getOrElse(throw new InvalidRequest("No JSON found in request"))
val validatedJSON = json.validate[Inner]
Ok("")
}
}
由于
答案 0 :(得分:0)
订单很重要。
case class Inner (deep: String)
object Inner {
implicit val reads = Json.reads[Inner]
}
case class Outer (in : Inner)
object Outer {
implicit val reads = Json.reads[Outer]
}
object Test extends Controller {
def tester = Action {
implicit request =>
val json = request.body.asJson.getOrElse(throw new InvalidRequest("No JSON found in request"))
val validatedJSON = json.validate[Inner]
Ok("")
}
}