我有一个 Scala 案例类
case class SmsGatewayConfig(oracle_id: Long,sms_type: String,seconds_of_high_prio: String,allowed_operators: String,allowed_organizations: String,phone_filter: String, min_throughput:Long,max_throughput: Long,comment: String,enabled: Boolean, isGwDedicated: bool 值)
和伴随对象
object SmsGatewayConfig {
implicit val smsGatewayConfig: Reads[SmsGatewayConfig] = (
(JsPath \ "oracle-id").read[Long] and
(JsPath \ "sms-type").read[String] and
(JsPath \ "seconds-of-high-prio").read[String] and
(JsPath \ "allowed-operators").read[String] and
(JsPath \ "allowed-organizations").read[String] and
(JsPath \ "phone-filter").read[String] and
(JsPath \ "min-throughput").read[Long] and
(JsPath \ "max-throughput").read[Long] and
(JsPath \ "comment").read[String] and
(JsPath \ "enabled").read[Boolean] and
(JsPath \ "isGwDedicated").read[Boolean]
)(SmsGatewayConfig.apply _)
}
和 JsValue 就像字符串中的 {"oracle-id":6877851,"sms-type":["1-100"],"allowed-operators":["all","unknown"],"phone-filter":["+7xxxxxxxxxx"],"max-throughput":1000000,"comment":"","enabled":false}
但是如果我尝试val obj=configs.as[SmsGatewayConfig]
我得到太多异常[JsResultException: JsResultException(errors:List((/allowed-operators,List(ValidationError) (error.expected.jsstring,WrappedArray()))), (/isGwDedicated,List(ValidationError(error.path.missing,WrappedArray()))), (/min-throughput,List(ValidationError(error.path.missing) ,WrappedArray()))), (/phone-filter,List(ValidationError(error.expected.jsstring,WrappedArray()))), (/sms-type,List(ValidationError(error.expected.jsstring,WrappedArray()) ))), (/允许的组织,List(ValidationError(error.path.missing,WrappedArray()))), (/秒-of-high-prio,List(ValidationError(error.path.missing,WrappedArray()) )))))]
我不知道我应该做什么来解决这个问题。抱歉我的英语不好
最佳答案
在 JSON 中,sms-type
、allowed-operators
和 phone-filter
是数组。
因此,您需要将 Reads 对象中的以下内容更改为:
object SmsGatewayConfig {
implicit val smsGatewayConfig: Reads[SmsGatewayConfig] = (
(JsPath \ "oracle-id").read[Long] and
(JsPath \ "sms-type").read[Seq[String]] and
(JsPath \ "seconds-of-high-prio").read[String] and
(JsPath \ "allowed-operators").read[Seq[String]] and
(JsPath \ "allowed-organizations").read[String] and
(JsPath \ "phone-filter").read[Seq[String]] and
(JsPath \ "min-throughput").read[Long] and
(JsPath \ "max-throughput").read[Long] and
(JsPath \ "comment").read[String] and
(JsPath \ "enabled").read[Boolean] and
(JsPath \ "isGwDedicated").read[Boolean]
)(SmsGatewayConfig.apply _)
}
您还需要更新案例类以表示这些字段是数组而不仅仅是字符串:
case class SmsGatewayConfig(oracle_id: Long, sms_type: Seq[String], seconds_of_high_prio: String, allowed_operators: Seq[String], allowed_organizations: String, phone_filter: Seq[String], min_throughput: Long, max_throughput: Long, comment: String, enabled: Boolean, isGwDedicated: Boolean)
此外,您的 JSON 输入中似乎缺少一些字段:seconds-of-high-prio
、allowed-organizations
、min-throughput
和 isGwDedicated
。
要在代码中表示这一点,请在案例类中使用 Option
并在 Reads 对象中使用 readNullable
:
case class SmsGatewayConfig(oracle_id: Long, sms_type: Seq[String], seconds_of_high_prio: Option[String], allowed_operators: Seq[String], allowed_organizations: Option[String], phone_filter: Seq[String], min_throughput: Option[Long], max_throughput: Long, comment: String, enabled: Boolean, isGwDedicated: Option[Boolean])
object SmsGatewayConfig {
implicit val smsGatewayConfig: Reads[SmsGatewayConfig] = (
(JsPath \ "oracle-id").read[Long] and
(JsPath \ "sms-type").read[Seq[String]] and
(JsPath \ "seconds-of-high-prio").readNullable[String] and
(JsPath \ "allowed-operators").read[Seq[String]] and
(JsPath \ "allowed-organizations").readNullable[String] and
(JsPath \ "phone-filter").read[Seq[String]] and
(JsPath \ "min-throughput").readNullable[Long] and
(JsPath \ "max-throughput").read[Long] and
(JsPath \ "comment").read[String] and
(JsPath \ "enabled").read[Boolean] and
(JsPath \ "isGwDedicated").readNullable[Boolean]
)(SmsGatewayConfig.apply _)
}
您遇到的所有错误,或者至少是大部分错误,都应该随着这些更改而消失。
作为一个小细节,我建议使用驼峰命名法,而不是用于命名案例类字段的基于下划线的命名。
关于json - Scala Play 2 - 将 JsValue 转换为模型对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27212494/