Play 2.3.x Scala - How to render json data to view?

I come from a Node world and I am having trouble getting my head moving my json to a view and then displaying the data.

I am pushing api to get 2 profiles. Res.body is json. The answer is pretty big, but now I only want to display some data in my 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>

      

I can display my json as string or even as json. But I cannot access the values ​​by keys. I just want to display 3 or 4 elements from each player as html. Then I can clean it up with css later.

+3


source to share


1 answer


You can access the json using code similar to this:

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

      



Or even:

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

      

+7


source







All Articles