json格式的数据显示到html中 利用pre code

缘由:需要将参数(json)展示在html中,查看.
出现问题:json挤到一起没有格式。使用JSON.stringify,并配置缩进。这两个不可少。然后使用

<pre>

三者必须配合。

HTML:
<pre>
    <code id="show"></code>
</pre>

JS:
var show = document.getElementById('show'),
    newData = JSON.stringify(arr, null, 4);
show.innerHTML = newData;

结果:
在这里插入图片描述

arr = [{
    id: 1,
    desc: "This is the description of the first object."
}, {
    id: 2,
    desc: "This is the description of the second object."
}, {
    id: 3,
    desc: "This is the description of the third object. Now, I'm going to repeat the description of this value.This is the description of the third objectThis is the description of the third objectThis is the description of the third object"
}, {
    id: 4,
    desc: "This is the description of the third object."
}]

参考:
https://www.jianshu.com/p/db8d77b3de9a

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值