K
K
Kanonier82015-05-14 11:25:17
JavaScript
Kanonier8, 2015-05-14 11:25:17

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>');
  }
}


It all works, of course, but am I doing everything right? maybe there are other ways to render JSON? More fast and flexible.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
Nikita K., 2015-05-14
@Kanonier8

For this, template engines are used, for example Handlebars, there is also a simple template engine in the underscore library.

S
Sergey, 2015-05-14
@serega_kaktus

you can still use the search here How to competently and quickly turn a Json string into an HTML structure?

K
Kirill Penzin, 2015-05-14
@kir_vesp

First link from google: json2html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question