1

I have written the following code for the printing the JSON in a HTML page.

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $.getJSON("http://localhost:8080/json",function(result){
    $.each(result, function(i, field){
      $("tr").append(field + " ");
    });
  });
});
</script>
</head>
<body>
  <table id="table">
    <tr>
      <th>market</th>
      <th>buy</th>
      <th>sell</th>
      <th>currency</th>
      <th>volume</th>
    </tr>
  </table>
  <div></div>
</body>
</html>

I am able to print the JSON Response values, but was not able to properly print them in a table.

JSON Response format:

{
  "market": 1309480,
  "buy": 1309480,
  "sell": 1280017,
  "currency": "INR",
  "volume": 2253.4518854
}

Currently how the Table is looking:

enter image description here

2
  • Don't you need td inside the tr? Also you are appending them to the same place your headers are. You should make use of thead and tbody. Commented Dec 12, 2017 at 10:45
  • @N.Ivanov Can you elaborate a bit. Commented Dec 12, 2017 at 10:46

3 Answers 3

2

You need <td> tag to display the content in the table. <th> is a table header tag.

Try below:

$.getJSON("http://localhost:8080/json",function(result){
        var tr = '<tr>';  //create tr tag 
        $.each(result, function(i, field){
            tr += '<td>' + field +'</td>';  //loop through the result and create <td>
        });
        tr += '</tr>';  //close tr tag
        $('#table').append(tr);  // append it to table
}

Also, I am pretty sure you might need this fro multiple columns (i.e if you result is multiple). You need to use 2 loops for that. Try it out. Created fiddle for reference: https://jsfiddle.net/bipen/3prfj474/ ;)

Sign up to request clarification or add additional context in comments.

5 Comments

can you help me with the HTML code as well. As I am not able to see any output, using the changes suggested by you.
@AlapanDas Wait, there was a syntax error. Updated the answer please check.
@AlapanDas Also, I think you want to display it in multiple columns if you result is an array. here is an example fiddle. jsfiddle.net/bipen/3prfj474
I am not able to get the output with the code you suggested. I have written the code here. Can you look into it once and let me know where am I making the mistake. jsfiddle.net/xt3pt6h2
dude!!! data should be result. Here update the fiddle. jsfiddle.net/bipen/xt3pt6h2/2 . Also, this will work if your result is array else the one above will work.
1

Here you go with a solution

var result = {
  "market": 1309480,
  "buy": 1309480,
  "sell": 1280017,
  "currency": "INR",
  "volume": 2253.4518854
};

//$.getJSON("http://localhost:8080/json",function(result){
  $('#table').append('<tr/>');
  $.each(result, function(i, field){
    $('#table').find('tr:last').append('<td>' + field + '</td>');    
  });
//});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="table">
  <tr>
    <th>market</th>
    <th>buy</th>
    <th>sell</th>
    <th>currency</th>
    <th>volume</th>
  </tr>
</table>
<div></div>

I just commented out the AJAX call, please uncomment & remove the result variable.

First append a tr to the table & then using jQuery last selector get the last appended tr & append the table to td data.

Hope this will help you.

2 Comments

@shailaditya Thanks it worked for me. Just a quick ques, I want to insert a one a static value like the first element of the row, how can I do that ?
@AlapanDas Similarly you can use :first
0

You may insert tag <td> in

$("tr").append(field + " ");

like this:

$("tr").append("<td>" + field + "</td>");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.