0

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

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,List(ValidationError(List) (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(List(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,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(15)/pull_request_title,List(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(List(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(List(error.expected.jsstring),WrappedArray()))), (/builds(22) /pull_request_number,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(23)/pull_request_title,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(23)/pull_request_number,List(ValidationError(List(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())))))

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

4

1 回答 1

2

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

implicit val buildFormat = Json.Reads[Build]

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

于 2016-03-08T19:47:55.937 回答