【问题标题】:Converting JsValue to Model in Scala Play2在 Scala Play2 中将 JsValue 转换为模型
【发布时间】:2015-10-15 01:11:20
【问题描述】:

当我尝试将 JsValue JSON 对象转换为对象时,出现以下错误

play.api.libs.json.JsResultException: JsResultException(errors:List((/_id/$oid,List(ValidationError(List(error.path.missing),WrappedArray()))))) (JsReadable.scala:20)

我传递的 JSON:

val environmentBody = Json.parse("""{
                       "name" : "Demo Value",
                       "schedulerUrl" : "https://ops-dev.videri.com/v1/scheduler/players/%s/schedules",
                       "playersUrl" : "https://ops-dev.videri.com/v1/crm/players",
                       "token" : "68a1fab4c911206165f3e426b50468eaa66193b56eee66ec508bf9304e6c8b92"
                   }""")

和转换:

val envObject = environmentBody.as[Environment]

环境模型如下所示:

case class Environment(id:Option[String] = None,
                       name: String,
                       schedulerUrl: String,
                       playersUrl: String,
                       token: String) extends Model{
  def getId() = id
} 

trait Model {
  def getId():Option[String]
}

我不确定为什么这不起作用。据我所知,这不应该引发任何验证错误。

【问题讨论】:

  • 我在我的项目中尝试过(使用 play 2.4),效果很好。
  • @mfirry 知道这个错误可能意味着什么吗?

标签: json scala playframework-2.0


【解决方案1】:

您的代码对我有用。我唯一做的就是把这行:

implicit val reads = Json.reads[Environment]

【讨论】:

  • 不知道,实际上。标识符'/_id/$oid',在异常中被引用为缺失对我来说看起来很奇怪。也许问题是由您的格式化程序/阅读器引起的?你是怎么定义的?
  • 我没有做任何特别的事情:implicit val environmentReads = Json.reads[Environment]implicit val environmentWrites = Json.writes[Environment]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-26
  • 2013-09-15
  • 1970-01-01
  • 1970-01-01
  • 2021-07-31
  • 2018-04-19
相关资源
最近更新 更多