如何在Scala中解析Play框架2.2中的JSON列表或数组 [英] How to parse json list or array in scala for play framework 2.2

查看:133
本文介绍了如何在Scala中解析Play框架2.2中的JSON列表或数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一些RESTful API测试用例,并且很少使用scala播放框架.

I am writing some RESTful API test cases and have little experience working with the scala playframwork.

这是我的JSON的示例.

Here is an example of my 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调用.

As you can see I have a list/Array of JSON items. I want to grab the id for the "morrowplots.jpg" file and store that into a variable to be used for successful API calls.

因此,我将代码设置为如下所示.下面代码中的结果变量是您在上面看到的JSON字符串.

So I setup my code to look like the following. The result variable in the code below is the JSON string you see above.

  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())
      }
    }

这给了我以下错误.

[info] +错误: {"obj size":[{"msg":"error.path.result.multiple","args":[]}],"obj filename":[{"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:[]}]}

[info] + Errors: {"objsize":[{"msg":"error.path.result.multiple","args":[]}],"objfilename":[{"msg":"error.path.resul t.multiple","args":[]}],"objid":[{"msg":"error.path.result.multiple","args":[]}],"objcontent-type":[{"msg":"error.path .result.multiple","args":[]}],"obj*datecreated":[{"msg":"error.path.missing","args":[]}]}

那么我该如何解决此列表/数组问题以及如何按文件名搜索以获取ID?

So how do I fix this List/Array issue and how do I search by filename to get the id?

提前谢谢.

推荐答案

我不是Play方面的专家,因此这可能不是惯用语言,但是它应该可以解决您的问题.首先,您的json是date-created,而您的scala期望的是datecreated.其次,您的Reads只应使用一个斜杠.接下来,您需要针对List[FileName]运行validate.

I am no expert on Play, so this might not be idiomatic however it should solve your problem. First, your json is date-created versus your scala expecting datecreated. Second, you should only use one slash for your Reads. Next, you need to run validate against a List[FileName].

关于文件名搜索,您现在可以从JsSuccess中提取列表,并对它运行filter.

As to the filename search, you can now extract the list from the JsSuccess and run a filter against it.

最终代码看起来像这样

case class FileName(size: String, datecreated: String, id: String, contenttype: String, filename: String)

  implicit val fileReads: Reads[FileName] = (
  (__ \ "size").read[String] and
  (__ \ "date-created").read[String] and
  (__ \ "id").read[String] and
  (__ \ "content-type").read[String] and
  (__ \ "filename").read[String]
  )(FileName)

  val json: JsValue = Json.parse(contentAsString(result))

  val nameResult = json.validate[List[FileName]]

  val list = nameResult match {
      case JsSuccess(list : List[FileName], _) => list
      case e: JsError => {
        info("Errors: " + JsError.toFlatJson(e).toString())
        List()
      }
    }

  list filter(_.filename contains "morrow")

这篇关于如何在Scala中解析Play框架2.2中的JSON列表或数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆