从 JSON 访问多个数组

发布于 2024-12-05 20:16:12 字数 408 浏览 5 评论 0原文

我有一些带有 elements 属性的 JSON,其中一个 JSON 元素具有多个数组的值。我已经弄清楚如何访问该数组(如果有一个数组),但我不确定如何访问第二个数组。

我还可以了解如何将新数组推送到 JSON 值。

这是我所拥有的

    $(this).attr({
"data-settings": $.toJSON({ nPosX: newPX, nPosY: newPY, oPosX: oldPX, oPosY: oldPY, moves: ["val"]["key"] })
});

var movesObj = $.parseJSON($(this).attr('data-settings'));

moves 是保存数组的地方。

提前致谢!

I have some JSON withing an elements attribute and one of the JSON elements has a value of multiple arrays. I have worked out how to access the array if there is one but I'm not sure how to access the second one.

I could also do with knowing how to push a new array to the JSON value.

Here is What I have

    $(this).attr({
"data-settings": $.toJSON({ nPosX: newPX, nPosY: newPY, oPosX: oldPX, oPosY: oldPY, moves: ["val"]["key"] })
});

var movesObj = $.parseJSON($(this).attr('data-settings'));

The moves is where the arrays are kept.

Thanks in advanced!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

好久不见√ 2024-12-12 20:16:12

好吧,这对我来说似乎不是一个有效的 JSON。您不能这样定义 JSON。您可以像散列一样定义移动:

moves: {"val":"key"}

您可以像以下方式访问它: moves.valmoves["val"]

或者您像散列数组一样定义移动:

moves: [{"val":"key"}, ...]

您可以访问它例如:moves[0].valmoves[0]["val"]

如果您想要这样的嵌套哈希,则需要像这样定义它:

moves: {"val": {"key":"value"}}

并像 moves["val"]["key"]moves.val.key 一样访问它>

Well, that does not seem like a valid JSON to me. You can't define a JSON like that. You either define moves like an hash:

moves: {"val":"key"}

you access it like: moves.val or moves["val"]

Or you define moves like an array of hashes:

moves: [{"val":"key"}, ...]

You access it like: moves[0].val or moves[0]["val"].

If you want a nested hash like that, you need to define it like:

moves: {"val": {"key":"value"}}

And access it like moves["val"]["key"] or moves.val.key

魂ガ小子 2024-12-12 20:16:12

您能更具体地说明您正在尝试做什么吗?

如果您有嵌套数组并且想要访问内部数组之一的值,则语法为

myArray["key1"]["key2"]

myArray.foo.bar

Could you be more specific about what you are trying to do?

If you have nested arrays and you want to access a value from one of the inner arrays, the syntax is

myArray["key1"]["key2"]

or

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