访问 JSON 对象数组中的特定值

发布于 2024-12-19 06:37:01 字数 423 浏览 2 评论 0原文

我正在使用带有backbone.js 0.53的rails 3,并且当前收到带​​有以下数组的GET:

[{"credit_card":
    {"id":2,"cc_number":"12345678912345","cc_type":"stack","owner":"overflow"}},
    ....next objects....]

我已经阅读了很多其他线程,但不知道如何访问这些值。 有什么方法可以使用 .get() 这样的骨干给定方法来做到这一点吗?

我尝试过

myArray = eval(arrayJSON)
alert myArray.length #works

,但访问数组中的单个值或迭代它的任何其他方法都会失败。 也许我只是在这里遗漏了一些东西。

I am using rails 3 with backbone.js 0.53 and currently receive a GET with the following array:

[{"credit_card":
    {"id":2,"cc_number":"12345678912345","cc_type":"stack","owner":"overflow"}},
    ....next objects....]

I have read a lot of the other threads but can't figure out how to access the values.
Is there any way to do this with the backbone-given methods like .get()?

I tried

myArray = eval(arrayJSON)
alert myArray.length #works

but any other way of accessing the single values in an array or iterating over it fails.
Probably I am just missing something here.

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

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

发布评论

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

评论(2

九厘米的零° 2024-12-26 06:37:01

如何通过下划线迭代所有结果的快速示例:

/* received results mocking */

model.attributes = [
    { "credit_card" : { "id":2, "cc_number":"12345678912345" },
    { "credit_card" : { "id":3, "cc_number":"44444444455555" },
    { "credit_card" : { "id":4, "cc_number":"66666655554332" }
]

/* lets get all results */

results = model.toJSON()

/* loop through all results */

_(results).each(item) {
    console.log(item.credit_card.id);
}

/* get result by array pos */

console.log(results[1]); // get 2nd item

干杯

Quick example of how to iterate through all your results via underscore:

/* received results mocking */

model.attributes = [
    { "credit_card" : { "id":2, "cc_number":"12345678912345" },
    { "credit_card" : { "id":3, "cc_number":"44444444455555" },
    { "credit_card" : { "id":4, "cc_number":"66666655554332" }
]

/* lets get all results */

results = model.toJSON()

/* loop through all results */

_(results).each(item) {
    console.log(item.credit_card.id);
}

/* get result by array pos */

console.log(results[1]); // get 2nd item

Cheers

请你别敷衍 2024-12-26 06:37:01

查看更多详细信息可能很有用,但一般来说,您应该能够使用以下语法访问元素: alert(myArray[0].credit_card.cc_number);

我什至粘贴了您的示例数组放入 jsFiddle 中,没有任何问题: http://jsfiddle.net/P4w7T/1/

It might be useful to see more details, but in general you should be able to access elements just fine with the following syntax: alert(myArray[0].credit_card.cc_number);

I even pasted your sample array into jsFiddle and had no issues: http://jsfiddle.net/P4w7T/1/

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