使用Scala中的Play Framework转换原始Json(Transform original Json using Play Framework in Scala)

系统教程 行业动态 更新时间:2024-06-14 17:04:03
使用Scala中的Play Framework转换原始Json(Transform original Json using Play Framework in Scala)

我有以下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.

更多推荐

本文发布于:2023-04-24 21:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/2066ce81e2b67664701d92ead13747c4.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:原始   Framework   Play   Scala   Transform

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!