从 JSON 访问多个数组
我有一些带有 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,这对我来说似乎不是一个有效的 JSON。您不能这样定义 JSON。您可以像散列一样定义移动:
您可以像以下方式访问它: moves.val 或 moves["val"]
或者您像散列数组一样定义移动:
您可以访问它例如:moves[0].val 或 moves[0]["val"]。
如果您想要这样的嵌套哈希,则需要像这样定义它:
并像 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:
you access it like: moves.val or moves["val"]
Or you define moves like an array of hashes:
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:
And access it like moves["val"]["key"] or moves.val.key
您能更具体地说明您正在尝试做什么吗?
如果您有嵌套数组并且想要访问内部数组之一的值,则语法为
或
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
or