Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to find data in json by jquery

Tags:

json

jquery

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);
});
like image 370
Mou Avatar asked Nov 28 '25 01:11

Mou


2 Answers

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.

like image 112
Beginner Avatar answered Nov 29 '25 19:11

Beginner


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);
});
like image 23
Arsh Singh Avatar answered Nov 29 '25 19:11

Arsh Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!