function jsonToHtml(data) {
var result = "<ol>";
for (var key in data) {
if (typeof (data[key]) == 'object' && data[key] != null) {
result += "<li><span class='key'>" + key + "</span>:<ul>";
result += jsonToHtml(data[key]);
result += '</ul></li>';
}
else {
result += "<li><span class='key'>" + key + '</span>: ';
result += '<span class="string">' + data[key] + '</span></li>';
}
};
result += '</ol>';
return (result);
}
27 August 2015
JS - Convert a (nested) JSON object to HTML
For testing purposes I wanted to display the JSON retrieved from an API in a HTML page. I used Fiddler for that, but after some security was included that became impossible. The following Javascript function converts the JSON to a ordered list tree:
Labels:
Javascript,
JSON
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment