简体   繁体   English

如何从“key:value”格式的键访问 json 值?

[英]How to access json value from key which is “key:value” format?

This is my payload object received in my backend running node.js.I want to access username testdoctor using key cognito:username .However key is again in form of key:value format.How do i access it?这是我在运行 node.js 的后端收到的有效负载 object。我想使用 key cognito:username访问用户名testdoctor 。但是 key 再次采用 key:value 格式。我如何访问它?

payload.email gives the email value but payload.' payload.email 给出 email 值,但有效载荷。 cogito:username ' doesn't do the trick. cogito:username ' 不能解决问题。

{
  'cognito:username': 'testdoctor',
  exp: 1621155498,
  iat: 1621151899,
  email: 'aayushbakliwal.2625@gmail.com'
}

When a property names contains restricted characters that are not allowed in a Javascript identifier (the colon in this case), then you have to use the bracket syntax for accessing the property name:当属性名称包含 Javascript 标识符(在本例中为冒号)中不允许的受限字符时,您必须使用方括号语法来访问属性名称:

payload['cognito:username']

Obviously, it is easier to program if you you don't have code that uses these restricted characters in property names, but if you're stuck with it, then you can use the bracket syntax as shown above.显然,如果您没有在属性名称中使用这些受限字符的代码,那么编程会更容易,但如果您坚持使用它,那么您可以使用如上所示的括号语法。 The same would be true if the property name contained a space too.如果属性名称也包含空格,情况也是如此。

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

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