[英]How to get JSON object values with the JSON keys having spaces in Node.js
This is my JSON object : 这是我的JSON对象:
var EVENT_ID = {
"Enable Popup Blocker": "Sol_EnablePopupBlocker_IE",
"Disable Script Debug": "Sol_DisableScriptDebugger_IE",
"Clear History": "Sol_ClearHistory_IE",
"Reset Settings": "Reset_InternetExplorer_Settings_SA",
"Profile Issue": "Fix_Outlook_Profile_SA",
"Send Receive": "Fix_Send_Receive_Errors_Outlook_SA",
"Search Issue": "Fix_Search_Outlook_SA"
};
I am trying to access the JSON object value with keys, which has spaces as shown below 我正在尝试使用键访问JSON对象值,该键具有如下所示的空格
var eventID = JSON.stringify(req.body.result.parameters.solution);
var aptEventName = EVENT_ID[eventID];
eventID
value is "Profile Issue"
eventID
值为"Profile Issue"
When I log my aptEventName
variable, it throws values as undefined
. 当我记录
aptEventName
变量时,它会将值抛出为undefined
。 Can anyone please tell me, where I am going wrong? 谁能告诉我,我要去哪里错了?
ONE POSSIBLE CASE: when you again do JSON.stringify()
on a string value this could happen. 一个可能的情况:当您再次对字符串值执行
JSON.stringify()
,可能会发生这种情况。 That's why it throws values as undefined , so don't use unnecessary JSON.stringify()
here 这就是为什么它会将值抛出为undefined ,所以请不要在这里使用不必要的
JSON.stringify()
var response = "response"
JSON.stringify(response)
""response""
^^ ^^ see extra quotes here
var result = { "source": "agent", "resolvedQuery": "LPTP-KDUSHYANT", "speech": "", "action": "gethostname", "actionIncomplete": false, "parameters": { "solution": "Profile Issue", "hostname": "LPTP-KDUSHYANT" }} var eventID = result.parameters.solution; var EVENT_ID = { "Enable Popup Blocker": "Sol_EnablePopupBlocker_IE", "Disable Script Debug": "Sol_DisableScriptDebugger_IE", "Clear History": "Sol_ClearHistory_IE", "Reset Settings": "Reset_InternetExplorer_Settings_SA", "Profile Issue": "Fix_Outlook_Profile_SA", "Send Receive": "Fix_Send_Receive_Errors_Outlook_SA", "Search Issue": "Fix_Search_Outlook_SA" }; var aptEventName = EVENT_ID[eventID]; console.log(aptEventName)
JSON.stringify
called on a string will return the string in quotes. 在字符串上调用
JSON.stringify
将返回用引号引起来的字符串。 You should not JSON-encode your key. 您不应该对密钥进行JSON编码。 And as string typecast is implicit when performing object field access you can type it just as:
而且由于字符串类型转换在执行对象字段访问时是隐式的,因此可以将其键入为:
var eventID = req.body.result.parameters.solution;
var aptEventName = EVENT_ID[eventID];
You should not be using stringify, it is taking a string and turning that string into JSON. 您不应该使用stringify,它使用一个字符串并将该字符串转换为JSON。
var eventID = JSON.stringify(req.body.result.parameters.solution);
when you do this your string is going to be 当您这样做时,您的字符串将是
var eventID = "\"Profile Issue\"";
So of course you have no properties in your object with quotes. 因此,当然您的对象中没有带引号的属性。 So what you need to do is drop the stringify bit and just have to reference the property in your object.
因此,您需要做的是删除字符串化位,而只需引用对象中的属性。
var eventID = req.body.result.parameters.solution;
var aptEventName = EVENT_ID[eventID];
You can get the values using: 您可以使用以下方法获取值:
EVENT_ID["Clear History"]
I think you have something wroong with the req.body.result.parameters.solution
. 我认为您对req.body.result.parameters.solution感到有些
req.body.result.parameters.solution
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.