访问 JSON 对象数组中的特定值
我正在使用带有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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如何通过下划线迭代所有结果的快速示例:
干杯
Quick example of how to iterate through all your results via underscore:
Cheers
查看更多详细信息可能很有用,但一般来说,您应该能够使用以下语法访问元素:
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/