2

如何在 Play 中构建 Reads!2.1.1 哪个读取这个JSON?下面的示例和代码引发异常。我实际上可以在 Play 中运行它!控制台,但如果我启动服务器并使用 curl 我会收到错误消息。

{
  "title": "title", 
  "description": "description", 
  "categories": [1,3], 
  "sections": [2]
}

按照 Play 上的指南进行操作!网站让我创建了这个“解析器”,但它抛出了一个异常。

  implicit val guideReads: Reads[GuideInstance.Update] = (
    (__ \ "title").readNullable[String] ~
    (__ \ "description").readNullable[String] ~
    (__ \ "categories").readNullable(
      Reads.list[Int]
    ) ~
    (__ \ "sections").readNullable(
      Reads.list[Int]
    )
  )(GuideInstance.Update)

例外

java.lang.NullPointerException: null
    at play.api.libs.json.Json$.fromJson(Json.scala:90) ~[play_2.10.jar:2.1.1]
    at play.api.libs.json.DefaultReads$$anon$2$$anonfun$10.apply(Reads.scala:453) ~[play_2.10.jar:2.1.1]
.......
    at play.api.libs.json.JsValue$class.validate(JsValue.scala:73) ~[play_2.10.jar:2.1.1]
    at play.api.libs.json.JsObject.validate(JsValue.scala:159) ~[play_2.10.jar:2.1.1]
    at controllers.mtadmin.GuidesController$$anonfun$update$1.apply(GuidesController.scala:114) ~[na:na]
    at controllers.mtadmin.GuidesController$$anonfun$update$1.apply(GuidesController.scala:114) ~[na:na]
4

1 回答 1

1

应该够用了readNullable[List[Int]]

implicit val guideReads: Reads[GuideInstance.Update] = (
  (__ \ "title").readNullable[String] ~
  (__ \ "description").readNullable[String] ~
  (__ \ "categories").readNullable[List[Int]] ~
  (__ \ "sections").readNullable[List[Int]]
)(GuideInstance.Update)

如果没有,请提供更完整的示例,我会帮助您!

于 2014-02-17T04:17:55.410 回答