javascript - 将简单的 JSON 对象转换为格式化的 HTML(javascript 或 jQuery)

标签 javascript jquery

我有一堆这样的对象:

"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/

相关文章:

javascript - Jquery slideToggle 弄乱了 flex 对齐,直到它完成

javascript - warning.js :45 Warning: setState(. ..):只能更新已安装或正在安装的组件

javascript - SetInterval 不从间隔递增

javascript - 如何获取所有带有 "click"事件的元素与 jQuery 相关联?

javascript - 准备淡入后

javascript - Jquery添加和删除子类

javascript - CSS3 过渡 : replace them entirely on mobile?

javascript - jqueryUI - 将项目拖到多个列表

javascript - 仅 SVG attr( y ) 不能立即起作用

javascript - 如何使用 jQuery 将光标从指针更改为手指?