进入Play框架中的JSON数组 [英] Stepping into JSON Arrays in Play Framework

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

问题描述

我正在尝试通过远程http响应在play框架中解析一些json.我正在尝试进入结果[0]->位置[0]-> latLng-> lat.我正在使用带有scala的playframework 2.0.

I am trying to parse through some json in the play framework from a remote http response. I am trying to get into results[0]->locations[0]->latLng->lat. I am using playframework 2.0 with scala.

下面是我正在使用的代码,并提供了一些到目前为止我已经尝试过的示例的注释示例.

Below is the code I am using with a few commented examples of what i've tried so far.

  val promise = WS.url("http://www.mapquestapi.com/geocoding/v2/address?...").get()
  val body = promise.value.get.body
  val json = Json.parse(body)
  val maybeLat = (json \ "results" \ "0" \ "locations" \ "0" \ "latLng" \ "lat").asInstanceOf[String]
  //val maybeLat = (json \ "results[0]" \ "locations[0]" \ "latLng" \ "lat").asInstanceOf[String]
  //val maybeLat = (json \ "results(0) \ "locations(0) \ "latLng" \ "lat").asInstanceOf[String]

  Ok(body).withHeaders(CONTENT_TYPE -> "text/json")

我从游戏框架中遇到的错误: http://pastebin.com/S5S3nY5D 我要解析的JSON: http://pastebin.com/7rfD0j2n

Errors I'm getting from play framework: http://pastebin.com/S5S3nY5D JSON That i'm trying to parse: http://pastebin.com/7rfD0j2n

推荐答案

我认为更好的方法是:

val resultsArray = (json \ "results").as[JsArray]
val locations = resultsArray \\ "locations"

这时,locations将是您可以遍历的对象列表,而无需通过其索引手动访问它们.

at this point, locations will be a list of objects that you can traverse, without having to access them manually through their index.

这篇关于进入Play框架中的JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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