简体   繁体   中英

Displaying JSON object array in HTML as response from Jersey

I am trying to display a JSON array in HTML which I get as response from Jersey.

I am getting JSON array as follows:

{"balance":
         [
          {"Group":"new","balance":" 10","description":"Cost vehicles"},
          {"Group":"new","balance":"677","description":"Motor vehicles"}
         ]
}

How can I display this kind of JSON in HTML as a table?

Resource methods are as follow:

@GET
@Produces("application/json")
@Path("/view")
public ArrayList<BalanceSheet>  viewBalanceSheet(){

ArrayList <BalanceSheet>  balanceList=BalanceSheetService.getBalanceSheet();

    return balanceList;
}

If you are using jQuery/Ajax to make the call you could do something to this effect:

HTML

<table id="your-table">
    <thead>
        <tr>
            <td>Name</td><td>Group</td><td>Balance</td><td>Description</td>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>

jQuery

$.getJSON('rest/service/view', function (data) {
    $.each(data, function (k, arr) {
        $('#your-table tbody').append('<tr><td>' + k + '</td><td>' + arr.Group + '</td><td>' + arr.balance + '</td><td>' + arr.description + '</td></tr>');
    });
});

Hope this helps. Let me know if I am way off base here.

With jQuery you could do something like this:

HTML :

<table>
   <tr></tr>
</table>

jQuery code (it works on how many hashes you have in your json array):

var heads = yourJson.ballance[0];
for (colHead in heads) {
    $('table tr:first-child').append('<th>').html(col);
}


for (var i=0; i<yourJson.ballance.length; i++) {
   var row = yourJson.ballance[i];
   for (col in row) {
      $('table').append('<tr>').append('<td>').html( row[col] );
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM