i tried this but did not work and getting error SyntaxError: invalid property id
js fiddle link 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);
});
Keys are case sensitive so id and Id are different. Also correct your JSON array. Try this:
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);
});
Here is the jsfiddle.
Your json array is invalid it should be :
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" }
];
and inside $.each id should be Id :
$.each(data, function(i, v) {
alert(v.Id);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With