【问题标题】:Get filtered JSON value Scala Play获取过滤后的 JSON 值 Scala Play
【发布时间】:2019-09-30 14:31:49
【问题描述】:

我使用 scala play 并使用 WS 从 URL 获取响应。

我的 JSON 示例:

[
 {
   "object": "001",
   "object-description": "MODEL",
   "criterion": "TW3",
   "criterion-description": "MODELE X07"
 },
{
  "object": "002",
  "object-description": "TYPE",
  "criterion": "STANDA",
  "criterion-description": "STANDARD TYPE"
}, ...

我只想获得“对象”等于“002”的“标准”字段。因此,在本例中,值为“STANDA”。

一个测试:

   ws.url(
    url)
  .get()
  .map { response =>
     Right((response.json \ "object="002"" \\ "criterion").map(_.as[String]))
  }

我该怎么做?

感谢您的帮助。

【问题讨论】:

    标签: scala playframework play-json ws


    【解决方案1】:

    您可以使用自动格式化程序将整个响应转换为 scala 类,然后对其进行操作。

    case class Data(`object`: String, criterion: String)
    implicit val dataRead = Json.reads[Data]
    
    response.json.as[List[Data]]
      .filter(_.`object` == "002")
      .map(_.criterion)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-14
      • 2020-09-04
      • 2016-07-12
      相关资源
      最近更新 更多