[英]JavaScript JSON object with square brackets and spaces in name
I have encountered a JSON Structure I have never seen before.我遇到了一个从未见过的 JSON 结构。 This is using Square Bracket ([]) but has spaces in the name.这是使用方括号 ([]) 但名称中有空格。 How would I navigate such a structure?我将如何导航这样的结构?
"profile": [{
"[Profile ID]": 1001398965,
"[Name | Prefix]": "Ms.",
"[Name | First]": "Lori",
"[Name | Middle]": "",
"[Name | Last]": "Smith",
"[Name | Suffix]": "",
"[Contact Name]": "Lori Smith"
},
{ "[Profile ID]": 1001398965,
"[Name | Prefix]": "Ms.",
"[Name | First]": "Jeanine",
"[Name | Middle]": "",
"[Name | Last]": "Samson",
"[Name | Suffix]": "",
"[Contact Name]": "Jeanine Samson"
}]
I have tried我努力了
profile[0]['Name | First'] //result undefined
profile[0][Name | First] //result Name is not defined
Any help is appreciated.任何帮助表示赞赏。
Brackets are part of the key.括号是键的一部分。 You have to use:你必须使用:
profile[0]['[Name | First]']
j['profile'][0]['[Name | Prefix]']
See this snippet:看到这个片段:
var j = { "profile": [{ "[Profile ID]": 1001398965, "[Name | Prefix]": "Ms.", "[Name | First]": "Lori", "[Name | Middle]": "", "[Name | Last]": "Smith", "[Name | Suffix]": "", "[Contact Name]": "Lori Smith" }, { "[Profile ID]": 1001398965, "[Name | Prefix]": "Ms.", "[Name | First]": "Jeanine", "[Name | Middle]": "", "[Name | Last]": "Samson", "[Name | Suffix]": "", "[Contact Name]": "Jeanine Samson" }] }; console.log(j['profile'][0]['[Name | Prefix]']);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.