scala – Play框架:读取包含空值的Json
发布时间:2020-12-16 09:51:22 所属栏目:安全 来源:网络整理
导读:我正在尝试在Play Scala程序中读取Json数据. Json在某些字段中可能包含空值,所以这就是我定义Reads对象的方式: implicit val readObj: Reads[ApplyRequest] = ( (JsPath "a").read[String] and (JsPath "b").read[Option[String]] and (JsPath "c").r
我正在尝试在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 _) 和ApplyRequest案例类: case class ApplyRequest ( a: String,b: Option[String],c: Option[String],d: Option[Int],) 这不编译,我得到类型为Option [String]的No Json反序列化器.尝试为此类型实现隐式读取或格式. 如何声明Reads对象接受可能的null? 解决方法
您可以使用
readNullable 来解析缺失或空字段:
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 _) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- Couldn't resolve host或者Name or service
- Ionic2 / Angular2 / TypeScript:如何访问函数2
- unix – 如何判断是否从交互式shell运行makefile
- 下拉菜单 – 如何在悬停时制作Bootstrap的下拉菜
- 如何在Angular 4中实现Google Maps API(initMap)
- 查询天气预报的Webservice接口程序源代码
- angular – 如何在typescript中的泛型类中创建类
- Scala通过将函数作为参数传递隐式参数来感受adnv
- 查询天气预报的Webservice接口程序源代码
- scala api design:我可以避免不安全的演员表或泛
热点阅读