【问题标题】:Store JSON without brackets and quotes存储不带括号和引号的 JSON
【发布时间】:2017-12-01 05:14:10
【问题描述】:

所以我有一个 Nodejs 服务器,它接收一个 JSON 对象,然后将它放入一个变量中并通过电子邮件发送该内容。这是我的代码:

app.post('/sendEmail', function(req, res) {
	var answers = req.body.answers;
	var str = JSON.stringify(answers, null, "\t"); // stringify with tabs inserted at each level
	console.log(answers);
	var fromEmail = new helper.Email('ahun...ok.com');
	var toEmail = new helper.Email('ahun...ok.com');
	var subject = 'Survey Completion';
	var content = new helper.Content('text/plain', str);
	
	
	var mail = new helper.Mail(fromEmail, subject, toEmail, content);

	var sg = require('sendgrid')(process.env.SENDGRID_API_KEY);
	var request = sg.emptyRequest({
	  method: 'POST',
	  path: '/v3/mail/send',
	  body: mail.toJSON()
	});
	
	sg.API(request, function (error, response) {
	  if (error) {
	    console.log('Error response received');
	  }
	});
	
	res.send("Success");
	
});

如您所见,我使用 stringify 使 JSON 更漂亮,但它仍然具有 JSON 格式的所有大括号、引号和间距。有没有办法以更易读的形式存储 JSON?

邮件内容在这里: var content = new helper.Content('text/plain', str); 所以可读的 JSON 需要存储在具有格式等的变量中。

任何帮助将不胜感激。谢谢

编辑:这是发送过来的 JSON 对象:

{

"question1": {
	"Reliable": true,
	"Knowledgeable": true,
	"Helpful": true,
	"Courteous": true
},
"question2": {
	"checked": "Extremely Well"
},
"question3": {
	"checked": "Extremely Well"
},
"question4": {
	"checked": 3
},
"fullName": "Test",
"address": "Test",
"city": "Test",
"state": "Test",
"zip": "321",
"areaCode": "321",
"phone": 1234567896,
"call": true,
"lifeInsurance": "Yes",
"brokerage": "Yes",
"bankName": "Regions"
}

使用上面的 JSON 对象,我想把它格式化成这样:

问题 1:可靠、知识渊博、乐于助人、彬彬有礼。

问题 2:非常好。

问题 3:非常好。

问题 4:3。

全名:测试。

地址:测试。

....

经纪:是的。

银行名称:地区。

【问题讨论】:

  • 嗯......你想发送什么?
  • 我想发送 JSON 对象,但不要让它看起来像上面的对象。我正在尝试对其进行格式化,使其看起来像是刚刚输入到电子邮件中。
  • 这可能吗?
  • 您能否在在问题中显示您希望输出格式是什么 - 说您不希望它看起来像什么没有帮助 - 是的,当然有可能
  • 我添加了一个输出格式的例子。对不起,我尽量说清楚。感谢您的评论。

标签: javascript html json node.js email


【解决方案1】:

更简单的答案可能是解析 JSON,然后将其转储为 YAML:

---
question1:
  Reliable: true
  Knowledgeable: true
  Helpful: true
  Courteous: true
question2:
  checked: Extremely Well
question3:
  checked: Extremely Well
question4:
  checked: 3
fullName: Test
address: Test
city: Test
state: Test
zip: '321'
areaCode: '321'
phone: 1234567896
call: true
lifeInsurance: 'Yes'
brokerage: 'Yes'
bankName: Regions

有几个用于 Node.js 的 YAML 库,我不记得我喜欢哪个,但我想说我对 js-yaml 很幸运。

【讨论】:

    【解决方案2】:

    数据只是需要一点按摩

    var data = {
        "question1": {
            "Reliable": true,
            "Knowledgeable": false,
            "Helpful": true,
            "Courteous": true
        },
        "question2": {
            "checked": "Extremely Well"
        },
        "question3": {
            "checked": "Extremely Well"
        },
        "question4": {
            "checked": 3
        },
        "fullName": "Test",
        "address": "Test",
        "city": "Test",
        "state": "Test",
        "zip": "321",
        "areaCode": "321",
        "phone": 1234567896,
        "call": true,
        "lifeInsurance": "Yes",
        "brokerage": "Yes",
        "bankName": "Regions"
    };
    
    var result = Object.entries(data).reduce((result, [key, value]) => {
        key = key.replace(/([A-Z]|\d+)/g, ' $1').replace(/^(.)/, (unused, p1) => p1.toUpperCase());
        if (!['string', 'number', 'boolean'].includes(typeof value)) {
            value = Object.entries(value).map(([key, value]) => (typeof value == 'boolean') ? (value ? key : undefined) : value).filter(v => v !== undefined).join(',');
        }
        result.push(`${key}: ${value}`);
        return result;
    }, []);
    console.log(result.join('\n'));

    注意:我将其中一个 true 更改为 false 以检查逻辑

    在节点 8.1.2 中测试

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-05
    • 2011-03-07
    • 1970-01-01
    • 2020-12-21
    • 2012-03-22
    • 2021-06-29
    • 2017-02-25
    相关资源
    最近更新 更多