2014-09-05 85 views
3

我來自節點世界,我無法包裝我的頭傳遞JSON到視圖,然後顯示數據。Play 2.3.x Scala - 如何在視圖中顯示json數據?

我點擊一個API來獲取2個配置文件。 res.body是json。響應相當大 - 但現在我只想在我的視圖中顯示一些數據。

 // 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清理它。

回答

7

您可以通過使用類似下面的代碼JSON訪問你:

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

甚至:

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