【问题标题】:JavaScript: How to format JSON to HTML to send email? [closed]JavaScript:如何将 JSON 格式化为 HTML 以发送电子邮件? [关闭]
【发布时间】:2021-03-22 15:50:05
【问题描述】:

在我的应用程序中,任何错误都会被捕获并通过电子邮件发送以供审核。我目前正在向电子邮件中添加标题和正文以帮助重现错误。它当前显示在单行中。我想以格式化的方式显示这一行,以便更容易阅读电子邮件。

我目前正在使用JSON.stringify(myJsonHere, null, 2) 来格式化我的 JSON。如果我使用console.log(JSON.stringify(myJsonHere, null, 2)) 来显示它,它确实可以工作,但是当我将它添加到像'<b>Headers:</b> ' JSON.stringify(myJsonHere, null, 2) 这样的HTML 电子邮件时它就不起作用了。

我的目标是获得这种格式:

{
  data: "test",
  id: 1,
  user: {
    id: 1,
    user: "test"
  }
}

而不是这种格式:

{data: "test", id: 1, user: {id: 1, user: "test"}}

【问题讨论】:

标签: javascript json formatting


【解决方案1】:

const pre = document.querySelector('pre');

pre.innerHTML = JSON.stringify({ test: 1, nested: { a: 1} }, null, 2)
<pre></pre>

【讨论】:

  • 谢谢你,我要试试这个
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-26
  • 2014-06-22
  • 2013-11-08
  • 2017-09-04
相关资源
最近更新 更多