我從一個web服務返回以下JSON:如何使用Play框架解析此JSON?
{
"hits": [
{
"created_at": "2016-02-01T15:01:03.000Z",
"title": "title",
"num_comments": 778,
"parent_id": null,
"_tags": [
"story",
"author",
"story_11012044"
],
"objectID": "11012044",
"_highlightResult": {
"title": {
"value": "title",
"matchLevel": "full",
"matchedWords": [
"title"
]
},
"author": {
"value": "author",
"matchLevel": "none",
"matchedWords": [
]
},
"story_text": {
"value": "Please lead",
"matchLevel": "none",
"matchedWords": [
]
}
}
}
]
}
,我試圖在遊戲框架使用JSON解析庫解析它。我有以下代碼:
import play.api.libs.functional.syntax._
import play.api.libs.json._
case class Post(id: Long, date: String, count: Int)
object Post {
implicit val postFormat = Json.format[Post]
implicit val writes: Writes[Post] = (
(JsPath \ "id").write[Long] and
(JsPath \"date").write[String] and
(JsPath \ "count").write[Int]
)(unlift(Post.unapply))
implicit val reads: Reads[Post] = (
(JsPath \ "objectID").read[Long] and
(JsPath \ "created_at").read[String] and
(JsPath \ "num_comments").read[Int]
)(Post.apply _)
}
import play.api.libs.json._
class PostRepo {
val request: WSRequest = ws.url(MY_URL)
def getPosts: Future[Seq[Post]] =
val result: Future[JsValue] = request.get().map(response =>
response.status match {
case 200 => Json.parse(response.body)
case _ => throw new Exception("Web service call failed: " + response.body)
})
result.map({
jsonvalue => println("JSARRAY: " + jsonvalue);
(jsonvalue \ "hits").as[Seq[Post]]
})
result
}
現在,當我運行代碼,我收到以下錯誤:
play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[JsResultException:
JsResultException(errors:List(((0)/date,List(ValidationError(List(error.path.missing),WrappedArray()))), ((0)/count,List(ValidationError(List(error.path.missing),WrappedArray()))), ((0)/id,List(ValidationError(List(error.path.missing),WrappedArray()))), ((1)/date,List(ValidationError(List(error.path.missing),WrappedArray()))), ((1)/count,List(ValidationError(List(error.path.missing),WrappedArray()))), ((1)/id,List(ValidationError(List(error.path.missing),WrappedArray()))), ((2)/date,List(ValidationError(List(error.path.missing),WrappedArray()))), ((2)/count,List(ValidationError(List(error.path.missing),WrappedArray()))), ((2)/id,List(ValidationError(List(error.path.missing),WrappedArray()))), ((3)/date,List(ValidationError(List(error.path.missing),WrappedArray()))), ((3)/count,List(ValidationError(List(error.path.missing),WrappedArray()))), ((3)/id,List(ValidationError(List(error.path.missing),WrappedArray())))
顯然東西是錯誤的我試圖解析JSON的方式但我現在花了幾個小時試圖弄清楚這個問題,並且我很好並且堅持不懈。
錯誤一些重構代碼顯示沒有從(__ \「命中」) –