我有以下Json:
{"id": 1, "url":[ {"format":""}, {"creator":""}, {"value":"http://..."} ] }如何将其转换为"url": "http://..." 。
我怎样才能做到这一点? 我尝试了以下,但它似乎没有工作。
(json \ "value").as[JsString].value
I have the following Json:
{"id": 1, "url":[ {"format":""}, {"creator":""}, {"value":"http://..."} ] }How can I transform it into "url": "http://...".
How can I do this? I tried the following, but it does not seem to be working.
(json \ "value").as[JsString].value
最满意答案
因为“url”具有数组值,所以需要对其进行适当的索引以获得所需的值,因此在您的情况下,它将类似于:
scala> val url = (json \ "url")(2).get url: play.api.libs.json.JsValue = {"value":"http://..."} scala> (url \ "value").as[String] res22: String = http://...如果您对处理JSON的更实用的方法感到满意,我还建议您查看Argonaut 。 它远远优于Play提供的JSON设施。
Because "url" has an array value, you need to index it appropriately to get the value you require, so in your case it would be something like:
scala> val url = (json \ "url")(2).get url: play.api.libs.json.JsValue = {"value":"http://..."} scala> (url \ "value").as[String] res22: String = http://...I also recommend looking at Argonaut if you are comfortable with a more functional approach for dealing with JSON. It is much superior to the JSON facilities provided by Play.
更多推荐
发布评论