使用数字作为“ index”(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这样的东西会让我的生活轻松很多。当然还有其他方法,但这种方法更好。

144412 次浏览

What about

Game.status[0][0] or Game.status[0]["0"] ?

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.

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.

var Game={
"status": [
{
"0": "val",
"1": "val",
"2": "val"
},
{
"0": "val",
"1": "val",
"2": "val"
}
]
}

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.

var Game={
"status": [
[
"val",
"val",
"val"
],
[
"val",
"val",
"val"
]
]
}

Probably you need an array?

var Game = {


status: [
["val", "val","val"],
["val", "val", "val"]
]
}


alert(Game.status[0][0]);

First off, it's not JSON: JSON mandates that all keys must be strings.

Secondly, regular arrays do what you want:

var Game = {
status: [
[
"val",
"val",
"val"
],
[
"val",
"val",
"val"
]
}

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.

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 is Game.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 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 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

In JavaScript, objects can be seen as a collection of properties. With the object literal syntax, a limited set of properties are initialized; then properties can be added and removed. Property values can be values of any type, including other objects, which enables building complex data structures. Properties are identified using key values. A key value is either a String or a Symbol value.