Javascript 使用数字作为“索引”(JSON)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8758715/
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-24 07:23:12  来源:igfitidea点击:

Using number as "index" (JSON)

javascriptjsonidentifier

提问by Zar

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.

最近开始研究 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);

Is there any way to do it the following way? Something like Game.status[0].0Would make my life way easier. Of course there's other ways around it, but this way is preferred.

有没有办法通过以下方式做到这一点?类似的东西Game.status[0].0会让我的生活更轻松。当然还有其他方法可以解决,但这种方法是首选。

回答by Quentin

JSON only allows key names to be strings. Those strings can consist of numerical values.

JSON 只允许键名是字符串。这些字符串可以由数值组成。

You aren't using JSON though. You have a JavaScript object literal. You canuse identifiers for keys, but an identifier can't start with a number. You can still use strings though.

不过,您没有使用 JSON。您有一个 JavaScript 对象字面量。您可以为键使用标识符,但标识符不能以数字开头。不过你仍然可以使用字符串。

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].

如果您使用点符号访问属性,则必须使用标识符。改用方括号表示法: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"
        ]
    ]
}

回答by yatskevich

Probably you need an array?

可能你需要一个数组?

var Game = {

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

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

回答by Amadan

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

首先,它不是 JSON:JSON 要求所有键都必须是字符串。

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).

会工作,如果你使用Game.status[0][0]. 不能使用带点符号 ( .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.

或者,您可以引用数字(即{ "0": "val" }...);您将拥有普通对象而不是数组,但相同的语法将起作用。

回答by zatatatata

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 mustuse the alternative notation, which is Game.status[0][0].

如果 Javascript 对象属性的名称既不以下划线也不以字母开头,则不能使用点表示法(如Game.status[0].0),而必须使用替代表示法,即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?

一个不同的注意事项,你真的需要它成为状态数组中的一个对象吗?如果您像使用数组一样使用对象,为什么不使用真正的数组呢?

回答by Huiguorou

JSON regulates key type to be string. The purpose is to support the dot notation to access the members of the object.

JSON 将密钥类型规定为字符串。目的是支持点符号来访问对象的成员。

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.

例如,person = {"height":170, "weight":60, "age":32}。您可以通过 person.height、person.weight 等访问成员。如果 JSON 支持值键,那么它看起来像 person.0、person.1、person.2。

回答by 2540625

JSON is "JavaScript Object Notation". JavaScript specifies its keys must be strings or symbols.

JSON 是“JavaScript 对象表示法”。JavaScript 指定其键必须是字符串或符号。

The following quotation from MDN Docs uses the terms "key/property" to refer to what I more often hear termed as "key/value".

以下来自 MDN Docs 的引用使用术语“键/属性”来指代我更常听到的称为“键/值”的内容。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Objects

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.

在 JavaScript 中,对象可以被视为属性的集合。使用对象字面量语法,初始化了一组有限的属性;然后可以添加和删除属性。属性值可以是任何类型的值,包括其他对象,这可以构建复杂的数据结构。使用键值标识属性。键值是字符串或符号值。

回答by Willem Mulder

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.

PS:你的问题是一个 Javascript 对象,而不是 JSON。JSON 是 Javascript 对象的“字符串”版本。