Skip to content

Instantly share code, notes, and snippets.

@riconegri
Created October 3, 2014 03:15
Show Gist options
  • Save riconegri/bfc5e12e566cfa0e9cc4 to your computer and use it in GitHub Desktop.
Save riconegri/bfc5e12e566cfa0e9cc4 to your computer and use it in GitHub Desktop.
write a clean html
var json = {
"div": {
"_style": "backgroung-color:#DC0600",
"_class": "btn btn-default",
"inner": {
span: {
_class: "warning",
inner: "bacana"
},
b: {
inner: "sim"
}
}
}
};
function json2html(obj) {
'use strict';
var x, y, body;
for (x in obj) {
if (obj.hasOwnProperty(x)) {
body = $('<' + x + '></' + x + '>');
for (y in obj[x]) {
if (obj[x].hasOwnProperty(y)) {
if (y.charAt(0) === '_') {
body.attr(y.replace('_', ''), obj[x][y]);
}
if (obj[x].inner instanceof Object) {
body.append(json2html(obj[x].inner));
} else {
body.html(obj[x].inner);
}
}
}
}
}
console.log(body);
return body;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment