我有一堆这样的对象:
"value": {
"something": true,
"another": false,
"testing": true,
"bob": "Hello",
"restrooms": true,
"price_rating": 1,
}
我只想将 value
中的项目呈现为 HTML。有点像:
"something": true
"another": false
"testing": true
"bob": "Hello"
"restrooms": true
"price_rating": 1
是否有一种简单的方法可以做到这一点,而无需编写一些复杂的 each
并一次附加每个项目?值得注意的是,这些对象将具有不同的属性和属性数量。
最佳答案
使用 JSON.stringify(obj, null, 2) 将对象转换为漂亮的字符串并将内容输出到 <pre>
标签
var a = {
"value": {
"something": true,
"another": false,
"testing": true,
"bob": "Hello",
"restrooms": true,
"price_rating": 1,
}
};
document.getElementById("output").innerHTML = JSON.stringify(a.value, null, 2);
<pre id="output"></pre>
关于javascript - 将简单的 JSON 对象转换为格式化的 HTML(javascript 或 jQuery),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45490650/