Scala play-json - 不 parse/deserialize 到单参数情况 class

Scala play-json - doesn't parse/deserialize to single argument case class

在 Scala 中,我想解析使用 Travis CI REST API 检索到的 JSON 响应。为此,我决定使用 play-json 库。除此之外,根据 REST API 'schema',我定义了以下情况 类:

case class Build(id : Int,
             repository_id : Int,
             commit_id : Int,
             number : String,
             event_type : String,
             pull_request : Boolean,
             pull_request_title : String,
             pull_request_number : String,
             state : String,
             started_at : String,
             finished_at : String,
             duration : Int,
             job_ids : List[Int]) extends Serializable{
}

case class Builds(builds : List[Build])

实施本身应该是不言自明的,但如果需要任何进一步的描述,我会更新问题。

除此之外,我还为 play-json 库定义了以下读物:

implicit val buildReads : Reads[Build] = (
      (JsPath \ "id").read[Int] and
      (JsPath \ "repository_id").read[Int] and
      (JsPath \ "commit_id").read[Int] and
      (JsPath \ "number").read[String] and
      (JsPath \ "event_type").read[String] and
      (JsPath \ "pull_request").read[Boolean] and
      (JsPath \ "pull_request_title").read[String] and
      (JsPath \ "pull_request_number").read[String] and
      (JsPath \ "state").read[String] and
      (JsPath \ "started_at").read[String] and
      (JsPath \ "finished_at").read[String] and
      (JsPath \ "duration").read[Int] and
      (JsPath \ "job_ids").read[List[Int]]
    )(Build.apply _)

implicit val buildsReads : Reads[Builds] = (__ \ "builds").read[List[Build]].map(Builds(_))

代码确实可以编译,模式即案例 类 确实确认了 REST API 响应,但是,在验证响应时,我收到以下错误:

JsError(List((/builds(0)/pull_request_title,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(0)/pull_request_number,List(ValidationError(List(error.expected.jsstring),WrappedArray())), (/builds(1)/pull_request_title,List(ValidationError(List(error.expected.jsstring) ,WrappedArray()))), (/builds(1)/pull_request_number,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(2)/pull_request_title,List(ValidationError(List(error.expected.jsstring),WrappedArray())), (/builds(2)/pull_request_number,List(ValidationError(List(error.expected.jsstring), WrappedArray()))), (/builds(3)/pull_request_title,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(3)/pull_request_number,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(4)/pull_request_title,List(ValidationError(List(error.expected.jsstring),WrappedArray ()))), (/builds(4)/pull_request_number,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(5)/pull_request_title,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(5)/pull_request_number,List(ValidationError(List(error.expected.jsstring),WrappedArray( )))), (/builds(6)/pull_request_number,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(7)/pull_request_title ,列表(验证错误(列表( error.expected.jsstring),WrappedArray()))), (/builds(7)/pull_request_number,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds (8)/pull_request_title,List(ValidationError(List(error.expected.jsstring),WrappedArray())), (/builds(8)/pull_request_number,List(ValidationError(列表(error.expected.jsstring),WrappedArray()))), (/builds(9)/pull_request_title,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds( 9)/pull_request_number,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(10)/pull_request_title,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(10)/pull_request_number,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(11 )/pull_request_number,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(12)/pull_request_title,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(12)/pull_request_number,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(13) /pull_request_title,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(13)/pull_request_number,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(14)/pull_request_title,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(14)/ pull_request_number,列表(瓦利dationError(List(error.expected.jsstring),WrappedArray()))), (/builds(15)/pull_request_title,列表(ValidationError(List(error.expected.jsstring),WrappedArray()))) , (/builds(15)/pull_request_number,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(16)/pull_request_number,List(ValidationError (List(error.expected.jsstring),WrappedArray()))), (/builds(17)/pull_request_number,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(18)/pull_request_title,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(18)/pull_request_number,List(ValidationError( List(error.expected.jsstring),WrappedArray()))), (/builds(19)/pull_request_title,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), ( /builds(19)/pull_request_number,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(20)/pull_request_title,List(ValidationError(列表(error.expected.jsstring),WrappedArray()))), (/builds(20)/pull_request_number,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/ builds(21)/pull_request_number,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(22)/pull_request_title,List(ValidationError(列表( error.expected.jsstring),WrappedArray()))), (/builds(22)/pull_request_number,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/build s(23)/pull_request_title,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(23)/pull_request_number,List(ValidationError(列表( error.expected.jsstring),WrappedArray()))), (/builds(24)/pull_request_title,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds (24)/pull_request_number,List(ValidationError(List(error.expected.jsstring),WrappedArray())))))

关于问题可能是什么的任何线索?提前致谢!

使用 play Json 如果您打算序列化的案例 class 没有使用任何您不需要手写您的读者或作者的自定义类型,您可以使用 Json.Writes[T]:

implicit val buildFormat = Json.Reads[Build]

这将自动为您生成 Reads(您也可以使用 Json.Writes[T] 或 Json.Format[T])。 Play 足够聪明,可以使用它来反序列化 List[T]。