Answer the question
In order to leave comments, you need to log in
How to parse and output JSON to HTML using JavaScript?
Greetings.
It is necessary to parse JSON, which is located on another site, and then read this json and display it as a table in html on the site.
I think that it is possible to do this using javascript using the JSON.parse method , but I have never worked with JavaScript and have not found an example of parsing exactly the site url.
And I do not fully understand how to output using innerHTML \ outerHTML
Please help. What is the easiest way to implement this and if there are examples, it would be ideal.
Thanks in advance!
Answer the question
In order to leave comments, you need to log in
For example:
var data = {
name: 'Jack',
items: {
dog: 1,
car: 1,
phone: 2,
},
};
// объект в json
var json = JSON.stringify(data); // {"name":"Jack","items":{"dog":1,"car":1,"phone":2}}
// json в объект
var obj = JSON.parse(json); // объект
var data = JSON.parse('{"name":"Jack","items":{"dog":1,"car":1,"phone":2}}');
var header = '<h2>My name is ' + data.name + '</h2>';
var list = '';
for (var i in data.items) {
list += '<li>' + i + ': ' + data.items[i] + ' шт. </li>';
}
document.getElementById('div').innerHTML += header;
document.getElementById('div').innerHTML += '<ul>' + list + '</ul>';
// Конечно, лучше создавать элементы через
// var div = document.createElement('div');
// наполнять через div.innerHTML = "text";
// и добавлять их через element.appendChild(div);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question