播放框架:读取包含空值的Json [英] Play framework: read Json containing null values

查看:69
本文介绍了播放框架:读取包含空值的Json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Play Scala程序中读取Json数据. Json的某些字段中可能包含空值,因此这就是我定义Reads对象的方式:

I'm trying to read Json data in my Play Scala program. The Json may contain nulls in some fields, so this is how I defined the Reads object:

  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 _) 

和ApplyRequest案例类:

And the ApplyRequest case class:

case class ApplyRequest ( a: String,
                          b: Option[String],
                          c: Option[String],
                          d: Option[Int],
                          )

无法编译,我得到No Json deserializer found for type Option[String]. Try to implement an implicit Reads or Format for this type.

如何声明Reads对象以接受可能的null?

How to declare the Reads object to accept possible nulls?

推荐答案

您可以使用

这篇关于播放框架:读取包含空值的Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆