Answer the question
In order to leave comments, you need to log in
How to properly convert JSON to HTML?
The title is probably a little "clumsy", but I will now reveal its essence.
In general, very often the data for filling a web page comes in JSON format, and in order to form HTML from it, I use something like this:
// подгрузил JSON файл
var jqxhr = $.getJSON( "test.json", function(data) {
_jsonParse(data);
console.log('sucsess')
})
.done(function() {
console.log('Done')
})
.fail(function() {
console.log('Error')
})
.always(function() {
console.log('End Ajax')
});
// функция парсинга JSON
function _jsonParse(data) {
for(var key in data) {
$('table').append(' \
<tr id="id' + key + '> \
<td>' + data[key].title +'</td> \
<td>' + data[key].image +'</td> \
<td>' + data[key].phone +'</td> \
<td>' + data[key].email +'</td> \
<td>' + data[key].desc +'</td> \
</tr>');
}
}
Answer the question
In order to leave comments, you need to log in
For this, template engines are used, for example Handlebars, there is also a simple template engine in the underscore library.
you can still use the search here How to competently and quickly turn a Json string into an HTML structure?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question