使用密钥名称访问 JSON 对象时出现问题。但适用于 []

issue accessing JSON object using key name with . but works with []

使用 访问 JSON 对象时,这是什么原因?给出错误但使用 [] 它有效吗?访问 JSON 对象键的正确语法是什么?

此语法给出错误:

var obj2G = obj.2G;

此语法有效,但为什么需要以这种方式访问​​?

var obj2G = obj["2G"];

var obj = {
  "2G": [{
    "essid": "SINGTEL-662F",
    "authmode": "psk psk2",
    "authkey": "0000026159",
    "isEnable": "1",
    "isHidden": "0",
    "hwaddr": "E0:8E:3C:00:66:30",
    "opmode": "ap"
  }, {
    "essid": "GUEST1-662F",
    "authmode": "psk psk2",
    "authkey": "aabbccddee",
    "isEnable": "0",
    "isHidden": "0",
    "hwaddr": "",
    "opmode": "ap"
  }, {
    "essid": "GUEST2-662F",
    "authmode": "psk psk2",
    "authkey": "aabbccddee",
    "isEnable": "0",
    "isHidden": "0",
    "hwaddr": "",
    "opmode": "ap"
  }, {
    "essid": "GUEST3-662F",
    "authmode": "psk psk2",
    "authkey": "aabbccddee",
    "isEnable": "0",
    "isHidden": "0",
    "hwaddr": "62:8E:3C:00:66:33",
    "opmode": "ap"
  }],
  "5G": [{
    "essid": "SINGTEL-662F(5G)",
    "authmode": "psk psk2",
    "authkey": "0000026159",
    "isEnable": "1",
    "isHidden": "0",
    "hwaddr": "E0:8E:3C:00:66:31",
    "opmode": "ap"
  }, {
    "essid": "GUEST1(5G)-662F",
    "authmode": "psk psk2",
    "authkey": "aabbccddee",
    "isEnable": "0",
    "isHidden": "0",
    "hwaddr": "6a:8e:3c:00:66:32",
    "opmode": "ap"
  }, {
    "essid": "GUEST2(5G)-662F",
    "authmode": "psk psk2",
    "authkey": "aabbccddee",
    "isEnable": "0",
    "isHidden": "0",
    "hwaddr": "",
    "opmode": "ap"
  }, {
    "essid": "GUEST3(5G)-662F",
    "authmode": "psk psk2",
    "authkey": "aabbccddee",
    "isEnable": "0",
    "isHidden": "0",
    "hwaddr": "6a:8e:3c:00:66:30",
    "opmode": "ap"
  }]
}

obj2G = obj["2G"];
console.log(obj2G);

obj2G = obj.2G;
console.log(obj2G);

对象属性在点号中使用时必须以字母开头。这只是Javascript中的一条规则。您可以按字面意思命名 属性 '*&^',但您必须通过 [] 符号来命名。

2G 以数字开头,因此需要 [] 符号。

从文档到 Property Accessor 点符号:

In this code, property must be a valid JavaScript identifier, i.e. a sequence of alphanumerical characters, also including the underscore ("_") and dollar sign ("$"), that cannot start with a number. For example, object. is valid, while object.1 is not.

然后到Variable

A JavaScript identifier must start with a letter, underscore (_), or dollar sign ($); subsequent characters can also be digits (0-9). Because JavaScript is case sensitive, letters include the characters "A" through "Z" (uppercase) and the characters "a" through "z" (lowercase).

结果是,不能使用以点号表示的变量或 属性。

如果您使用点符号:

property must be a valid JavaScript identifier, i.e. a sequence of alphanumerical characters, also including the underscore ("_") and dollar sign ("$"), that cannot start with a number. For example, object. is valid, while object.1 is not.

在这种情况下,您必须使用括号表示法:

property_name is a string. The string does not have to be a valid identifier; it can have any value, e.g. "1foo", "!bar!", or even " " (a space).

查看文档:https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Property_accessors

值得一提的是,如果要使用变量访问 属性,则必须使用括号表示法。例如:

var myName = "foo";
var myObject = { foo: 42};
console.log(myObject.myName);//returns undefined

但是:

var myName = "foo";
var myObject = { foo: 42};
console.log(myObject[myName]);//returns 42