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:
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.