일부 RESTful API 테스트 케이스를 작성하고 있으며 스칼라 플레이 프레임 작업에 대한 경험이 거의 없습니다.재생 프레임 워크 2.2에서 json 목록 또는 스칼라를 스칼라로 구문 분석하는 방법
다음은 JSON의 예입니다.
[ {
"size" : "5082",
"date-created" : "Wed Nov 19 17:10:39 CST 2014",
"id" : "546d236fb84e894eefd8f769",
"content-type" : "image/png",
"filename" : "chrome-on-windows.PNG"
}, {
"size" : "15684",
"date-created" : "Mon Jan 12 17:28:02 CST 2015",
"id" : "54b4588266b3d11b1c1e9db6",
"content-type" : "image/png",
"filename" : "logos_ncsa.png"
}, {
"size" : "1267871",
"date-created" : "Mon Jan 12 17:28:03 CST 2015",
"id" : "54b4588366b3d11b1c1e9dba",
"content-type" : "image/jpg",
"filename" : "morrowplots.jpg"
} ]
자세히 알 수 있듯이 JSON 항목 목록/배열이 있습니다. "morrowplots.jpg"파일의 ID를 가져 와서 성공적인 API 호출에 사용할 변수에 저장하려고합니다.
그래서 다음과 같이 코드를 설정했습니다. 아래 코드의 결과 변수는 위에서 보았던 JSON 문자열입니다.
case class FileName(size: String, datecreated: String, id: String, contenttype: String, filename: String)
implicit val fileReads: Reads[FileName] = (
(__ \\ "size").read[String] and
(__ \\ "datecreated").read[String] and
(__ \\ "id").read[String] and
(__ \\ "content-type").read[String] and
(__ \\ "filename").read[String]
)(FileName.apply _)
val json: JsValue = Json.parse(contentAsString(result))
val nameResult: JsResult[FileName] = json.validate[FileName](fileReads)
info("Right after validate")
nameResult match {
case s: JsSuccess[FileName] => {
val testfile: FileName = s.get
// Do something with testfile
info("Success")
}
case e: JsError => {
info("Error")
info("Errors: " + JsError.toFlatJson(e).toString())
}
}
이렇게하면 다음과 같은 오류가 발생합니다.
[정보] + 에러 : { "OBJ 크기"[{ "MSG": "error.path.result.multiple", "인수"[]}, "OBJ 파일명" : [{ "msg": "error.path.resul t.multiple", "args": []}] "obj id": [{ "msg": "error.path.result.multiple" "args": []}], "obj content-type": [{ "msg": "error.path .result.multiple", "args": []}], "obj * datecreated" [ "msg": "error.path.missing", "args": []}]}
이 목록/배열 문제를 어떻게 해결할 수 있으며 파일 이름별로 검색하여 ID를 얻는 방법은 무엇입니까?
미리 감사드립니다.