将json解析为Case Class

时间:2014-01-31 11:13:34

标签: json scala playframework

我有以下JSON,我需要将2个值映射到案例类 这些值为 job_id 状态

JSON如下

{
  "screenshots": [
    {
      "browser": "chrome",
      "state": "pending",
      "url": "http://localhost/mirror/52ea1b22e4b0e133507b209b",
      "browser_version": "26.0",
      "os_version": "7",
      "id": "92342eed1fd14c354d9365cbbd3e35ea1fc45df2",
      "os": "Windows"
    }
  ],
  "wait_time": 5,
  "callback_url": "http://localhost/screenshot/accept/52ea1b22e4b0e133507b209b",
  "quality": "compressed",
  "job_id": "ce991c0c3d140b5a78859b28cf391fd99c63ff98",
  "win_res": "1024x768",
  "orientation": "portrait",
  "mac_res": "1024x768"
}

案例类为

case class JobInfo(job_id: String)

object JobInfo {
  implicit val fmt = Json.format[JobInfo]
}

这工作正常,但我想将截图'state'添加到case类中而不必拥有整个scrernshot,因为我只是持久化job_id并声明如下所示

case class JobInfo(job_id: String, state: String)

object JobInfo {
  implicit val fmt = Json.format[JobInfo]
}

我正在阅读下面的响应,但截图是一个数组,所以我想知道如何从中提取JSON键'state'并将其映射到case类

    .map {
                response => {
    val jobInfo = Json.parse(response.body).as[JobInfo]    
     }
   }

1 个答案:

答案 0 :(得分:0)

您需要定义自定义Reads [JobInfo]对象来处理解析。假设您打算从屏幕截图数组中获取第一个元素以查找状态,您可以按如下方式执行:

import play.api.libs.json._
import play.api.libs.functional.syntax._

object JobInfo {
  implicit val jobReads: Reads[JobInfo] = (
    (__ \ "job_id").read[String] and
    ((__ \ "screenshots")(0) \ "state").read[String]
  )(JobInfo.apply _)
}

现在解析json的原始调用现在可以正常工作;但是,您应该使用validate方法来正确处理JSON未按预期格式化的情况,即:

Json.parse(response).validate[JobInfo].fold(
  jobInfo => {
    //do something with your jobInfo
    println(jobInfo)  
  },
  errors => {
    //handle JSON parsing errors
    println(errors)
  }
)

这样您可以更优雅地处理解析错误。 Play文档更详细地描述了这一点here