简体   繁体   English

如何获取节点js中JSON元素的值? 无法解析以下代码中的值

[英]How to get the value of a JSON element in node js? Unable to parse the value in below code

Below is the theJson string which I have converted to object by JSON.parse 以下是我已通过JSON.parse转换为对象的theJson字符串

var clientScopeJson={"cl1":{"List":"rwe","urlList":["nclsdlc","alkdcjla"]}};

JSON.hasOwnProperty(id) returns as true but JSON.id gives undefined: JSON.hasOwnProperty(id)返回true,但JSON.id给出了undefined:

id = "cl1"
//scope = JSON.parse(clientScopeJson);
console.log(clientScopeJson);
clientId = "cl1";
exists = clientScopeJson.hasOwnProperty(clientId); // This returns as true
console.log(exists);
scopeList = clientScopeJson.clientId;
console.log(scopeList);

If you want to access key that is saved in variable, you can access it like this 如果要访问保存在变量中的密钥,可以像这样访问

const clientId = "cl1";
const scopeList = clientScopeJson[clientId];

If you try to use it with dot, you are trying to access 'clientId' key. 如果尝试将其与点一起使用,则尝试访问“ clientId”键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM