对象javascript中数组中的引用对象
我希望这个标题有意义。。。我对使用Javascript做任何事情都很陌生,我已经搜索了一段时间了 我使用节点RED接收包含JSON的HTTP POST。我在msg.req.body中发布了以下数据,希望拉出对象javascript中数组中的引用对象,javascript,json,node-red,Javascript,Json,Node Red,我希望这个标题有意义。。。我对使用Javascript做任何事情都很陌生,我已经搜索了一段时间了 我使用节点RED接收包含JSON的HTTP POST。我在msg.req.body中发布了以下数据,希望拉出目标中的对象: { "policy_url": "https://alerts.newrelic.com/accounts/xxxxx/policies/7477", "condition_id": 429539, "condition_name": "Erro
目标中的对象
:
{
"policy_url": "https://alerts.newrelic.com/accounts/xxxxx/policies/7477",
"condition_id": 429539,
"condition_name": "Error rate",
"account_id": 773524,
"event_type": "INCIDENT",
"runbook_url": null,
"severity": "CRITICAL",
"incident_id": 50,
"version": "1.0",
"account_name": "Inc",
"timestamp": 1436451988232,
"details": "Error rate > 5% for at least 3 minutes",
"incident_acknowledge_url": "https://alerts.newrelic.com/accounts/xxxxxx/incidents/50/acknowledge",
"owner": "Jared Seaton",
"policy_name": "Default Policy",
"incident_url": "https://alerts.newrelic.com/accounts/xxxxxx/incidents/50",
"current_state": "acknowledged",
"targets": [{
"id": "6002060",
"name": "PHP Application",
"link": "https://rpm.newrelic.com/accounts/xxxxxx/applications/6002060?tw[start]=1436450194&tw[end]=1436451994",
"labels": {
},
"product": "APM",
"type": "Application"
}]
}
我想格式化通过TCP发送的字符串,以便将事件插入我们的事件管理系统。因此,我尝试了以下方法:
msg.payload = msg.req.body.targets[0] + "|" + msg.req.body.severity + "|" + msg.req.body.current_state + "|" + msg.req.body.details + "|" + msg.req.body.condition_name + "\n\n";
return(msg);
这将产生以下信息:
[object Object]|CRITICAL|acknowledged|Error rate > 5% for at least 3 minutes|Error rate
我尝试了一些不同的方法,但我要么得到null返回,要么得到[object]。感觉我很接近
有人能帮忙吗
提前感谢。
目标[0]
是一个对象,这就是您看到[object object]的原因
您应该改为执行msg.req.body.targets[0].name
以访问该对象的属性
结果消息将如下所示
PHP Application|CRITICAL|acknowledged|Error rate > 5% for at least 3 minutes|Error rate
您需要JSON.stringify()
这将把存储在目标数组第一个插槽中的对象转换为它的字符串表示形式
msg.payload = JSON.stringify(msg.req.body.targets[0]) + "|" + msg.req.body.severity + "|" + msg.req.body.current_state + "|" + msg.req.body.details + "|" + msg.req.body.condition_name + "\n\n";
return(msg);