循环使用多个对象的 json

发布于 2024-11-07 22:49:21 字数 1336 浏览 0 评论 0原文

我有一些 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 技术交流群。

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

发布评论

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

评论(4

命比纸薄 2024-11-14 22:49:21

尝试使用

for (var i=0; i<json.length; i++) {
   alert("JSON Data: " + json[i].pk_records_id);
  // you need to write each key name here
}

DEMO

try with

for (var i=0; i<json.length; i++) {
   alert("JSON Data: " + json[i].pk_records_id);
  // you need to write each key name here
}

DEMO

∞琼窗梦回ˉ 2024-11-14 22:49:21

从表面上看,您访问的结果不正确。

尝试:

success: function(obj_records){
    $.each(obj_records, function(index, value) {
        alert(value.pk_records_id); 
    });             
} 

From the looks of it, you're accessing the result incorrectly.

Try:

success: function(obj_records){
    $.each(obj_records, function(index, value) {
        alert(value.pk_records_id); 
    });             
} 
茶花眉 2024-11-14 22:49:21

我不确定(没有地方测试它),但我认为你想要:

success: function(obj_records){
    $.each(obj_records, function(index, value) {
        alert(value.pk_records_id); 
    });             
} 

I'm not sure (not in a place to test it), but I think you want:

success: function(obj_records){
    $.each(obj_records, function(index, value) {
        alert(value.pk_records_id); 
    });             
} 
江湖正好 2024-11-14 22:49:21

我相信您想要的是这样的:

success: function(obj_records){
    $.each(obj_records, function(index, value) {        
        alert(value.pk_records_id);     
    });             
} 

函数头内的 value 参数是您要循环的 obj_records 数组的单个对象。

I beleive what you want is this:

success: function(obj_records){
    $.each(obj_records, function(index, value) {        
        alert(value.pk_records_id);     
    });             
} 

the value parameter inside the function header is the individual object of the obj_records array that you are looping over.

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