[英]Scala Play handling JSON objects and arrays
I am making a third party API call using Play WS to get back a JSON object with a list of objects. 我正在使用Play WS进行第三方API调用,以获取带有对象列表的JSON对象。 I want to iterate through this list and add a key/value to each item.
我想遍历此列表,并向每个项目添加键/值。 I am new to Scala and typed languages, and it is really difficult to deal with JSON in Scala.
我是Scala和类型语言的新手,在Scala中处理JSON确实很困难。 Any help would be greatly appreciated.
任何帮助将不胜感激。 Thanks!
谢谢!
This is what I have right now: 这就是我现在所拥有的:
def getAll(filters: String) = Action.async { request =>
val api_url = API_URL + "search?api_key=" + API_KEY + filters
ws.url(api_url).get().map { response =>
val body = response.body
val json = Json.parse(body)
val listings = (json \ "listings").get
}
// I want to iterate through listings, which is of type JsValue
// And for each object, I want to add a key/value pair
Ok(listings)
}
Once you have a JsonNode you have to cast it to ObjectNode to be able to add a property. 拥有JsonNode之后,必须将其强制转换为ObjectNode才能添加属性。 For example, having listings: EDIT:
例如,具有列表:编辑:
listings.asInstanceOf[JsArray].value.map{ jsvalue =>
(jsvalue.asInstanceOf[JsObject]) + ("extra" -> JsString("something"))
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.