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

发布于 2024-12-25 06:47:24 字数 534 浏览 5 评论 0原文

最近开始深入研究 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

阳光下的泡沫是彩色的 2025-01-01 06:47:24

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

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

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

如果您使用点表示法访问属性,则必须使用标识符。请改用方括号表示法:Game.status[0][0]

但考虑到这些数据,数组似乎更有意义。

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

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"
        ]
    ]
}
著墨染雨君画夕 2025-01-01 06:47:24

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

其次,常规数组可以执行您想要的操作:

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

如果您使用 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:

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.

枫以 2025-01-01 06:47:24

也许你需要一个数组?

var Game = {

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

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

Probably you need an array?

var Game = {

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

alert(Game.status[0][0]);
我纯我任性 2025-01-01 06:47:24

当 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 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?

初心未许 2025-01-01 06:47:24

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.

白昼 2025-01-01 06:47:24

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

以下来自 MDN 文档的引用使用术语“键/属性”来指代我更常听到的术语“键/值”。

https://developer.mozilla.org/en-US/文档/Web/JavaScript/Data_structs#Objects

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

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.

我要还你自由 2025-01-01 06:47:24

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

其中一个有效吗

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文