【问题标题】:Scala JSON Reads in Play 2.1.1Scala JSON 在 Play 2.1.1 中读取
【发布时间】:2013-07-05 21:58:05
【问题描述】:

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

JSON:

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

斯卡拉:

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 的以下错误:

【问题讨论】:

  • 你可以试试(Price.apply _) 吗?

标签: json scala playframework-2.1


【解决方案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 _)

奇怪的是,只是通过案例类不起作用......

【讨论】:

  • 我认为是因为 reader 是在伴随对象中定义的。
猜你喜欢
  • 1970-01-01
  • 2013-06-07
  • 1970-01-01
  • 1970-01-01
  • 2016-07-12
  • 2016-03-31
  • 2013-07-11
  • 2013-01-20
  • 1970-01-01
相关资源
最近更新 更多