2.3 Scala Play框架 - 使用嵌入对象解析Json

时间:2014-09-28 16:06:37

标签: json scala parsing playframework

以下代码给出了

的编译错误
No implicit Reads for controllers.Inner available.

如果我改变

case class Outer (in : Inner)

case class Outer (in : String)

它编译得很好,我可以解析OuterInner 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("")


  }
}

由于

1 个答案:

答案 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("")

  }
}