JSON在Play scala 2.x中读取组合器不适用于Map [String,_]

时间:2015-03-09 19:48:23

标签: json scala playframework playframework-2.0

我有一个Play-scala v2.3应用程序。从this guide about Json Combinators开始,我试图这样做:

object Application extends Controller {

  case class Foo(id: String, docs: List[Map[String, _]])

  implicit val fooReads = (
    (JsPath \ "id").read[String] and
    (JsPath \ "docs").read[List[Map[String, _]]]
  )(Foo.apply _)

  ...
}

但后来我遇到了编译错误:

No Json deserializer found for type List[Map[String, _]]. Try to implement an implicit Reads or Format for this type.
       (JsPath \ "docs").read[List[Map[String, _]]]
                             ^

这是需要阅读的示例json:

{
    "id": "001",
    "docs": [
        {
            "name": "Billy",
            "level": 2,
            "marked": false
        },
        {
            "name": "Fred",
            "level": 5,
            "marked": true
        }
    ]
}

我也试过这个:

  case class Foo(id: String, docs: Map[String, _])

  implicit val fooReads = (
    (JsPath \ "id").read[String] and
    (JsPath \ "docs").read[Map[String, _]]
  )(Foo.apply _)

同样的错误。

似乎Play的JSON组合器不适用于Map类型。有谁知道如何解决这个问题?

1 个答案:

答案 0 :(得分:4)

你最好避免使用Map[String, Any],Scala和Play可以让你轻松一点。更好的解决方案是为您尝试表示为地图的事物定义自定义案例类:

import play.api.libs.json._

case class Doc(name: String, level: Int, marked: Boolean)
case class Foo(id: String, docs: List[Doc])

implicit val docFormat = Json.format[Doc]
implicit val fooFormat = Json.format[Foo]

val json = Json.parse(...)

然后:

scala> json.as[Foo]
res0: Foo = Foo(001,List(Doc(Billy,2,false), Doc(Fred,5,true)))

或者如果你想要更多控制:

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

implicit val fooReads: Reads[Foo] = (
  (__ \ 'id).read[String] and
  (__ \ 'docs).read(
    Reads.list((
      (__ \ 'name).read[String] and
      (__ \ 'level).read[Int] and
      (__ \ 'marked).read[Boolean]
    )(Doc.apply _))
  )
)(Foo.apply _)

如果您确实需要Map[String, Any],则可以随时在Doc上编写转换方法。