Visme
Visme

Reputation: 983

Create table using JSON in grails

I have a action that give the JSON. This is my JSON:

{"code":0,"message":"SUCCESS","packet":{"id":"1","name":"unit1"}}

In gsp page, I want to display this json as a table like this

Id Name

1  unit1

Please give me some ideas regarding this.

Upvotes: 2

Views: 1026

Answers (3)

MKB
MKB

Reputation: 7619

You can create a table dynamically and show in a div like:

jQuery.ajax({
    url:"${createLink(controller: 'testController', action: 'testAction')}",
    success:function (data) {
    jQuery("#testDiv").html("<table class='some-class'><tr><td>Id</td><td>Name</td></tr><tr><td>" +
               data.packet.id + "</td><td>" + data.packet.name + "</td></tr></table>");
    }
});

and

<div id="testDiv"></div>

Try it..,.

Upvotes: 0

Saurabh Dixit
Saurabh Dixit

Reputation: 633

You can display your JSON element(Handling JSON in the same way as MAP) on the GSP page using each loop on the packet, something like:

<table>
   <tr>
      <td>Id</td>
      <td>Name</td>
   </tr>
<g:each in="${receivedJson.packet}" var="packetInstance">
   <g:if test="${packetInstance}">
      <tr>
         <td>${packetInstance.id}</td>
         <td>${packetInstance.name}</td>
      </tr>
   </g:if>
</g:each>
</table>

Hope it may help :)

Upvotes: 1

Sudhir N
Sudhir N

Reputation: 4096

Given that your action given JsonElement, You can treat json as a map.

You can access id using

${jsonVar.packet.id}
and name using

${jsonVar.packet.name}

Upvotes: 0

Related Questions