我正在尝试使用 Scala 将JsArray
a 反序列化为List[T]
playframework 应用程序中的 a 。经过一些研究,我发现这种方法应该可以完成所需的工作:
/**
* Deserializer for List[T] types.
*/
implicit def listReads[T](implicit fmt: Reads[T]): Reads[List[T]] = new Reads[List[T]] {
def reads(json: JsValue) = json match {
case JsArray(ts) => ts.map(t => fromJson(t)(fmt)).toList
case _ => throw new RuntimeException("List expected")
}
}
问题是我不知道如何使用它。欢迎任何帮助。