Javascript 选择键中带有冒号的 JSON 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4925760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:48:30  来源:igfitidea点击:

Selecting a JSON object with a colon in the key

javascriptjson

提问by Eric Koslow

I'm using a third party tool that POSTs a JSON response. It works great, but one of the keys I need to use has a colon in it and I have no idea how to select this object in JavaScript.

我正在使用发布 JSON 响应的第三方工具。它工作得很好,但我需要使用的一个键中有一个冒号,我不知道如何在 JavaScript 中选择这个对象。

For example:

例如:

{
  "photo": {
    "reg": {
      "id": 50
    },
    "thumb": {
      "id": 51
    },
    ":original": {
      "id": 53"
    }
  }
}

How do I select photo.:original.id? I get syntax errors when I leave the colon in, and undefinedwhen I try dropping the colon.

我该如何选择photo.:original.id?当我将冒号留在里面时,以及undefined当我尝试删除冒号时,都会出现语法错误。

回答by kirilloid

It's simple:

这很简单:

photo[':original'].id

Dot/bracket notation

点/括号表示法