当前位置 : 主页 > 网页制作 > JQuery >

通过jquery在json中查找数据的最佳方法

来源:互联网 收集:自由互联 发布时间:2021-06-15
我试过这个但没有工作并得到错误SyntaxError:无效的属性id js小提琴链接http://jsfiddle.net/mm49u6wv/ var data={ { "Id": "12345", "Name": "Acme Widget 1", "Price": "£25.99", "Status": "In Stock" }, { "Id": "67890", "N
我试过这个但没有工作并得到错误SyntaxError:无效的属性id

js小提琴链接http://jsfiddle.net/mm49u6wv/

var data={
 { "Id": "12345", "Name": "Acme Widget 1", "Price": "£25.99", "Status": "In Stock"  },
 { "Id": "67890", "Name": "Acme Widget 2", "Price": "£28.99", "Status": "In Stock"  },
 { "Id": "11123", "Name": "Acme Widget 3", "Price": "£15.99", "Status": "In Stock"  },
 { "Id": "14156", "Name": "Acme Widget 4", "Price": "£33.99", "Status": "In Stock"  }
}

$.each(data, function(i, v) {
    alert(v.id);
});
键区分大小写,因此id和Id不同.还要更正您的JSON数组.试试这个:

var data= [
 { "Id": "12345", "Name": "Acme Widget 1", "Price": "£25.99", "Status": "In Stock"  },
 { "Id": "67890", "Name": "Acme Widget 2", "Price": "£28.99", "Status": "In Stock"  },
 { "Id": "11123", "Name": "Acme Widget 3", "Price": "£15.99", "Status": "In Stock"  },
 { "Id": "14156", "Name": "Acme Widget 4", "Price": "£33.99", "Status": "In Stock"  }
];

$.each(data, function(i, v) {
    alert(v.Id);
});

这是jsfiddle.

网友评论