Parse Json array response in scala\Play

人盡茶涼 提交于 2020-01-03 04:35:10

问题


There is a web service returning array of something

{"apps": [{"name": "one"}, {"name": "two"}]}

In my code I want to iterate every name

val request = WS.url(s"http://localhost:9000/getData")

val json = request.get.map { response =>
  (response.json \ "apps" \\ "name")
}

json.foreach(println)

However all my attempts return single record

// Expect
one
two
// Actual
ListBuffer("one", "two")

回答1:


First of all, the neat solution here would be:

val request = WS.url(s"http://localhost:9000/getData")

request.get.map { response =>
  val names = (response.json \ "apps" \\ "name")
  names.foreach(println)
}

Secondly, if you don't want to get confused about the types, you should change your naming standards. For a Future object, you may start with the prefix future, for an Option, it could start with maybe, etc. If you do so, the problem in your example will be more obvious:

val request = WS.url(s"http://localhost:9000/getData")

val futureJson = request.get.map { response =>
  (response.json \ "apps" \\ "name")
}

futureJson.foreach(println) // you call foreach for a Future, not for a List

Thirdly, why would Future trait have a method called foreach? I think that's confusing for beginners and even mid-level developers. We know from other languages that, foreach means iterate over a list of objects. In Scala, it is considered part of "Monadic operations" which is still a gray area for me :), but the comment for Future.foreach in Scala source is this:

  /** Asynchronously processes the value in the future once the value becomes available.
   *
   *  Will not be called if the future fails.
   */
  def foreach[U]



回答2:


Your value of json is actually a Future[Seq[JsValue]], so when you foreach over the future you get the entire list back. You would need an additional foreach to iterate over the list of values.



来源:https://stackoverflow.com/questions/34272826/parse-json-array-response-in-scala-play

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!