【发布时间】:2015-03-26 18:00:14
【问题描述】:
型号-
case class Renting(name: String, pets: Int)
case class Resident(renting: List[Renting])
case class Location(residents: List[Resident])
查看-
@(jsonResults: List[Renting])
@jsonResults.map { json =>
Name: @json.name
Pets: @json.pets
}
控制器 -
val json: JsValue = Json.obj(
"location" -> Json.obj(
"residents" -> Json.arr(
Json.obj(
"renting" -> Json.arr(
Json.obj(
"name" -> "John Doe",
"pets" -> 2
),
Json.obj(
"name" -> "Jane Smith",
"pets" -> 1
)
)
)
)
)
)
implicit val rentingFormat = Json.format[Renting]
implicit val residentFormat = Json.format[Resident]
implicit val locationFormat = Json.format[Location]
(json \ "location").validate[Location] match {
case s: JsSuccess[Location] => {
val location: Location = s.get
/* Something happens here that converts Location to List[Renting] */
Ok(views.html.index(location))
}
case e: JsError => Ok(JsError.toFlatJson(e))
}
根据s.get.toString 输出,似乎正在正确遍历json;但是,我需要将类型从Location 更改为List[Renting],以便我可以将结果传递到视图中。任何帮助将不胜感激!
【问题讨论】:
-
您应该检查
jsonResults的类型,以及它是否与您在视图中采用的类型相匹配。
标签: json scala playframework playframework-2.0 scala-2.10