我的目标是将API请求返回的JSON发送到Slack。当我发送一种内容类型的JSON时,我的代码有效。但是,我需要针对两种内容类型完成此操作。我想高效地解析两种内容类型的JSON,并使用单个代码块将JSON发送到Slack。下面的例子说明了。
Content
{
"Content": {
"CreatedByUser": {
"DisplayName": "<username>",
"ProfileUrl": "<profileurl>"
},
"HtmlName": "<name>",
"HtmlDescription": "<html>",
"Url": "<url>"
}
}
Comment
请注意,JSON块1是嵌套的:
{
"Comment": {
"Content": {
"CreatedByUser": {
"DisplayName": "<username>",
"ProfileUrl": "<profileurl>"
},
"HtmlName": "<name>",
"HtmlDescription": "<html>",
"Url": "<url>"
}
}
}
这显示了我当前的代码,该代码仅用 JSON块1 处理 。
提取值:
var request = require('request');
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
var info = JSON.parse(body);
var username = info.Content.CreatedByUser.Username;
var profileUrl = info.Content.CreatedByUser.ProfileUrl;
var subject = info.Content.HtmlName;
var url = info.Content.Url
var text = info.Content.HtmlDescription;
};
所有值都发送到Slack:
function sendToSlack(theUsername, theIconEmoji) {
var payload = {
attachments: [{
author_name: username,
author_link: profileUrl,
title: subject,
title_link: url,
text: text
}]
};
var theRequest = {
url: urlWebHook,
method: "POST",
json: payload
};
request(theRequest, function (error, response, body) { });
}
var urlWebHook = "<webhook url>";
sendToSlack();
相反,我希望代码能够处理JSON块1和2,而无需复制代码。例如,我相信我可以做类似的事情:
if (info.hasOwnProperty('Content')) {
var username = info.Content.CreatedByUser.Username;
var profileUrl = info.Content.CreatedByUser.ProfileUrl;
var subject = info.Content.HtmlName;
var url = info.Content.Url
var text = info.Content.HtmlDescription;
};
if (info.Comment.hasOwnProperty('Content')) {
var username = info.Comment.CreatedByUser.Username;
var profileUrl = info.Content.CreatedByUser.ProfileUrl;
var subject = info.Comment.HtmlName;
var url = info.Comment.Url
var text = info.Comment.Body;
};
...之后是将JSON发送到Slack的代码。
(我知道上面的说法不准确-我只是想传达我的想法。)
但是,如果我正确理解并基于实验,那将需要我在每个if
子句中复制“发送到Slack”代码,因为如果变量在if
子句之外,否则未将其视为已定义。取而代之的是,我只想使用一次“发送到Slack”代码来简化维护和使代码更简洁。
我是JavaScript新手。我被困在这一点上,不胜感激。
答案 0 :(得分:3)
正如Pointy所评论的那样,将用var
声明的变量视为在包含函数的顶部声明。这与ES6中用let
声明的变量相反。因此,您的代码按原样应该可以正常工作。您可能想定义变量而不在if
之前设置它们,因为有些人不喜欢在if块中声明var
>
var username, profileUrl, subject, url, text;
if (info.hasOwnProperty('Content')) {
username = info.Content.CreatedByUser.Username;
profileUrl = info.Content.CreatedByUser.ProfileUrl;
subject = info.Content.HtmlName;
url = info.Content.Url
text = info.Content.HtmlDescription;
};
if (info.hasOwnProperty('Comment')) {
username = info.Comment.CreatedByUser.Username;
profileUrl = info.Content.CreatedByUser.ProfileUrl;
subject = info.Comment.HtmlName;
url = info.Comment.Url
text = info.Comment.Body;
};
为使效率更高,您只能通过更改值从中提取的值来提取一次值:
var content;
if (info.hasOwnProperty('Content')) {
content = info.Content;
}
if (info.hasOwnProperty('Comment')) {
content = info.Comment.Content;
}
var username = content.CreatedByUser.Username;
var profileUrl = content.CreatedByUser.ProfileUrl;
var subject = content.HtmlName;
var url = content.Url
var text = content.HtmlDescription;