Skip to content

Instantly share code, notes, and snippets.

@jimfleming
Created August 25, 2010 15:46
Show Gist options
  • Save jimfleming/549747 to your computer and use it in GitHub Desktop.
Save jimfleming/549747 to your computer and use it in GitHub Desktop.
var json_escape = function(value) {
if (value.replace)
return value.replace(/\//g, '\\/')
else
return value
}
var json_to_html = function(json, level) {
level = level || 0
if (!level) {
$('body > pre').append('{\n')
json_to_html(json, 1)
$('body > pre').append('}\n')
} else {
var j = 0;
for (var i in json) {
var is_object = typeof json[i] == 'object'
$('body > pre').append('\t'.repeat(level) + '<strong>"' + i + '"</strong>:' + (!is_object ? '"<span class="no-whitespace">' + json_escape(json[i]) + '</span>"' + (j != json.length - 1 ? ',' : '') : '{') + '\n')
if (is_object)
json_to_html(json[i], level + 1)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment