循环使用多个对象的 json
我有一些 json 代码,其中包含多个对象,如下所示:
这些是对象内的值:
这是 json 代码:
[{"pk_records_id":"34","record_artist":"Bouncing Souls","record_title":"How I Spent My Summer Vacation","record_added_date":"2011-05-05 17:36:34","record_category":"punkrock","record_price":"11.00","record_cover_link":"img\/Bouncing Souls-How I Spent My Summer Vacation.jpg","record_amount_sold":null,"record_amount_stock":"400","record_description":"A great follow-up to Hopeless Romantic"},{"pk_records_id":"4","record_artist":"Descendents","record_title":"Everything Sucks","record_added_date":"2011-03-11 00:00:00","record_category":"punkrock","record_price":"12.00","record_cover_link":"img\/descendents_everything_sucks.jpg","record_amount_sold":"3124","record_amount_stock":null,"record_description":null}]
这是我尝试使用的代码,所以我能够检索值(显然):
success: function(obj_records){
$.each(obj_records, function(index, value) {
alert(obj_records.index.pk_records_id);
});
}
但这不起作用。我怎样才能检索数据?
编辑:
如果我使用此代码,我会为 json 代码中的每个字符获得一个数组。
$.each(obj_records, function(index, value) {
alert(index + " : " + value);
});
I have some json-code with has multiple objects in it, as such:
These are the values inside the object:
This is the json-code:
[{"pk_records_id":"34","record_artist":"Bouncing Souls","record_title":"How I Spent My Summer Vacation","record_added_date":"2011-05-05 17:36:34","record_category":"punkrock","record_price":"11.00","record_cover_link":"img\/Bouncing Souls-How I Spent My Summer Vacation.jpg","record_amount_sold":null,"record_amount_stock":"400","record_description":"A great follow-up to Hopeless Romantic"},{"pk_records_id":"4","record_artist":"Descendents","record_title":"Everything Sucks","record_added_date":"2011-03-11 00:00:00","record_category":"punkrock","record_price":"12.00","record_cover_link":"img\/descendents_everything_sucks.jpg","record_amount_sold":"3124","record_amount_stock":null,"record_description":null}]
And this is the code I try to use, so I would be able to retrieve the values (obviously):
success: function(obj_records){
$.each(obj_records, function(index, value) {
alert(obj_records.index.pk_records_id);
});
}
But this doesn't work. How can I retrieve the data?
Edit:
If I use this code, I get an array for every single character in my json-code.
$.each(obj_records, function(index, value) {
alert(index + " : " + value);
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试使用
DEMO
try with
DEMO
从表面上看,您访问的结果不正确。
尝试:
From the looks of it, you're accessing the result incorrectly.
Try:
我不确定(没有地方测试它),但我认为你想要:
I'm not sure (not in a place to test it), but I think you want:
我相信您想要的是这样的:
函数头内的
value
参数是您要循环的obj_records
数组的单个对象。I beleive what you want is this:
the
value
parameter inside the function header is the individual object of theobj_records
array that you are looping over.