ps0*_*604 5 scala playframework playframework-2.0
我正在尝试在Play Scala程序中读取Json数据.Json在某些字段中可能包含空值,所以这就是我定义Reads对象的方式:
implicit val readObj: Reads[ApplyRequest] = (
(JsPath \ "a").read[String] and
(JsPath \ "b").read[Option[String]] and
(JsPath \ "c").read[Option[String]] and
(JsPath \ "d").read[Option[Int]]
) (ApplyRequest.apply _)
Run Code Online (Sandbox Code Playgroud)
和ApplyRequest案例类:
case class ApplyRequest ( a: String,
b: Option[String],
c: Option[String],
d: Option[Int],
)
Run Code Online (Sandbox Code Playgroud)
这不能编译,我明白了 No Json deserializer found for type Option[String]. Try to implement an implicit Reads or Format for this type.
如何声明Reads对象接受可能的null?
您可以readNullable用来解析缺失或null字段:
implicit val readObj: Reads[ApplyRequest] = (
(JsPath \ "a").read[String] and
(JsPath \ "b").readNullable[String] and
(JsPath \ "c").readNullable[String] and
(JsPath \ "d").readNullable[Int]
) (ApplyRequest.apply _)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1993 次 |
| 最近记录: |