使用数字作为“索引” (JSON)
最近开始深入研究 JSON,目前我正在尝试使用数字作为“标识符”,但效果不太好。 foo:"bar"
工作正常,而 0:"bar"
则不行。
var Game = {
status: [
{
0:"val",
1:"val",
2:"val"
},
{
0:"val",
1:"val",
2:"val"
}
]
}
alert(Game.status[0].0);
有没有办法按照下面的方法来做呢?像 Game.status[0].0
这样的东西会让我的生活变得更轻松。当然还有其他方法,但这种方法是首选。
Recently started digging in to JSON, and I'm currently trying to use a number as "identifier", which doesn't work out too well. foo:"bar"
works fine, while 0:"bar"
doesn't.
var Game = {
status: [
{
0:"val",
1:"val",
2:"val"
},
{
0:"val",
1:"val",
2:"val"
}
]
}
alert(Game.status[0].0);
Is there any way to do it the following way? Something like Game.status[0].0
Would make my life way easier. Of course there's other ways around it, but this way is preferred.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
JSON 只允许键名是字符串。这些字符串可以由数值组成。
不过你没有使用 JSON。你有一个 JavaScript 对象文字。您可以使用键的标识符,但标识符不能以数字开头。不过您仍然可以使用字符串。
如果您使用点表示法访问属性,则必须使用标识符。请改用方括号表示法:
Game.status[0][0]
。但考虑到这些数据,数组似乎更有意义。
JSON only allows key names to be strings. Those strings can consist of numerical values.
You aren't using JSON though. You have a JavaScript object literal. You can use identifiers for keys, but an identifier can't start with a number. You can still use strings though.
If you access the properties with dot-notation, then you have to use identifiers. Use square bracket notation instead:
Game.status[0][0]
.But given that data, an array would seem to make more sense.
首先,它不是 JSON:JSON 要求所有键都必须是字符串。
其次,常规数组可以执行您想要的操作:
如果您使用 Game.status[0][0] ,则它将起作用。您不能使用带点符号 (
.0
) 的数字。或者,您可以引用数字(即
{ "0": "val" }...
);您将拥有普通对象而不是数组,但相同的语法将起作用。First off, it's not JSON: JSON mandates that all keys must be strings.
Secondly, regular arrays do what you want:
will work, if you use
Game.status[0][0]
. You cannot use numbers with the dot notation (.0
).Alternatively, you can quote the numbers (i.e.
{ "0": "val" }...
); you will have plain objects instead of Arrays, but the same syntax will work.也许你需要一个数组?
Probably you need an array?
当 Javascript 对象属性的名称不以下划线或字母开头时,您不能使用点符号(如
Game.status[0].0
),并且必须< /strong> 使用替代符号,即Game.status[0][0]
。另一个注意事项是,您真的需要它成为状态数组中的对象吗?如果您像数组一样使用该对象,为什么不使用真正的数组呢?
When a Javascript object property's name doesn't begin with either an underscore or a letter, you cant use the dot notation (like
Game.status[0].0
), and you must use the alternative notation, which isGame.status[0][0]
.One different note, do you really need it to be an object inside the status array? If you're using the object like an array, why not use a real array instead?
JSON规定key类型为字符串。目的是支持点表示法访问对象的成员。
例如,人 = {“身高”:170,“体重”:60,“年龄”:32}。您可以通过 person.height、person.weight 等访问成员。如果 JSON 支持值键,那么它看起来像 person.0、person.1、person.2。
JSON regulates key type to be string. The purpose is to support the dot notation to access the members of the object.
For example, person = {"height":170, "weight":60, "age":32}. You can access members by person.height, person.weight, etc. If JSON supports value keys, then it would look like person.0, person.1, person.2.
JSON 是“JavaScript 对象表示法”。 JavaScript 指定其键必须是字符串或符号。
以下来自 MDN 文档的引用使用术语“键/属性”来指代我更常听到的术语“键/值”。
https://developer.mozilla.org/en-US/文档/Web/JavaScript/Data_structs#Objects
JSON is "JavaScript Object Notation". JavaScript specifies its keys must be strings or symbols.
The following quotation from MDN Docs uses the terms "key/property" to refer to what I more often hear termed as "key/value".
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Objects
?
其中一个有效吗
PS:你的问题是一个Javascript对象,而不是JSON。 JSON 是 Javascript 对象的“字符串”版本。
What about
Does one of these work?
PS: What you have in your question is a Javascript Object, not JSON. JSON is the 'string' version of a Javascript Object.