在Scala Play2中将JsValue转换为Model

时间:2015-10-14 16:48:12

标签: json scala playframework-2.0

当我尝试将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]
}

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

1 个答案:

答案 0 :(得分:2)

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

implicit val reads = Json.reads[Environment]