格式化电子邮件正文的JSON

时间:2015-11-20 18:26:23

标签: javascript json regex pretty-print

我使用以下格式的JSON,我试图使用

插入到Outlook /电子邮件客户端的正文中
location.href = "mailto:" + "email@domain.com" + "?subject=" + "Notes" + "&body=" + JSON.stringify(data, null, 4);

给予4个空格JSON.stringify(data, null, 4)

[
    {
        "page": "article_0_page_0",
        "note": "Note 1"
    },
    {
        "page": "article_0_page_1",
        "note": "Note 2"
    }
]

我想输出为

<b>article_0_page_0</b>\n\r
Note 1

<b>article_0_page_1</b>\n\r
Note 2

请使用任何正则表达式解决方案。

编辑:我的尝试

var str = "";
        for(var i=0; i<data.length; i++) {
            str += "<strong>" + data[i].page + "</strong><br>" + data[i].note + "<br><br>";
        }

1 个答案:

答案 0 :(得分:3)

这里你去了

var json = [
    {
        "page": "article_0_page_0",
        "note": "Note 1"
    },
    {
        "page": "article_0_page_1",
        "note": "Note 2"
    }
];

var out = "";

for (var key in json) {
  if (json.hasOwnProperty(key)) {
      out += "<b>"+json[key].page+"</b>\r\n"+json[key].note+"\r\n\r\n";
  }
}

console.log(out);

此处示例:https://jsfiddle.net/q5r4gdcn/1/