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

JSON Reads combinator in Play scala 2.x doesn't work for Map[String, _]

我有一个 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 类型。有人知道如何解决这个问题吗?

避免使用 Map[String, Any] 会好很多,Scala 和 Play 使这变得很容易。更好的解决方案是为您要表示为地图的事物定义自定义案例 class:

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上写一个转换方法。