gpt4 book ai didi

json - Play 2.3.x Scala - 如何在 View 中显示 json 数据?

转载 作者:行者123 更新时间:2023-12-01 09:56:30 24 4
gpt4 key购买 nike

我来自 Node 世界,我无法将 json 传递给 View 然后显示数据。

我正在点击一个 api 来获取 2 个配置文件。 res.body 是 json。响应相当大 - 但现在我只想在我的 View 中显示一些数据。

      // Application.scala
val profile1 = WS.url(player1URL).get()
val profile2 = WS.url(player2URL).get()

Future.sequence(Seq(profile1, profile2)).map {
response => Ok(views.html.index.render(
Json.obj("player1" -> response(0).json, "player2" -> response(1).json)))
}


//index.scala.html
@(z: play.api.libs.json.JsObject)

<body>
@z.player1 //value player1 is not a member of play.api.libs.json.JsObject
// ideally I want
// z.player1.battleTag //displays battle tag
// z.player1.paragonLevel //displays paragon level
</body>

我可以将我的 json 显示为字符串甚至 json。但我无法通过键访问这些值。我只想将每个播放器的 3 或 4 个项目显示为 html。然后我可以稍后用一些 css 清理它。

最佳答案

您可以使用类似于以下的代码来访问您的 json:

@{(z\"player1"\"battleTag").as[String]}

甚至:

<script>
var jsono = @Html(z.toString) ;
alert(jsono.player1.battleTag)
</script>

关于json - Play 2.3.x Scala - 如何在 View 中显示 json 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25677556/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com