Scala JSON在Play 2.1.1中阅读

时间:2013-07-05 13:39:09

标签: json scala playframework-2.1

我正在尝试将一个超级简单的JSON读取到Scala对象:

JSON:

{"base": 100, "sale": 75}

Scala的:

import play.api.libs.json._
import play.api.libs.functional.syntax._

case class Price(base: Int, sale: Option[Int])

object Price {
  implicit val priceReads = (
    (__ \ "base").read[Int] ~
    (__ \ "sale").readNullable[Int]
  )(Price)
}

这给了我以下来自Play的错误:

enter image description here

1 个答案:

答案 0 :(得分:1)

试试这个:

  import play.api.libs.json._
  import play.api.libs.functional.syntax._

  implicit val priceReads:Reads[Price] = (
    (__ \ "base").read[Int] ~
    (__ \ "sale").readNullable[Int]
  )(Price.apply _)

奇怪的是,只是传递案例类不起作用......