I am trying to parse a JSON response from a PHP call.
The returned data is in JSON format, as below:
{"success":true,
"venues":
[
{"id":237,"title":"Country House Hotel and Restaurant","thumb_image":"thumb_combe-house-buttercups_3617067271.jpg","description":"","subType":1},
{"id":579,"title":"Abode Exeter","thumb_image":"wvs.jpg","description":"","subType":0},
{"id":484,"title":"Anran Luxury Boutique","thumb_image":"wvs.jpg","description":"","subType":0}
]
}
However, when I try to use the $.each
function to loop over it, it is returning an error Uncaught TypeError: Cannot read property 'length' of undefined
.
A snippet of the code I am using to perform the $.each loop is below.
$.each(data.venues, function() {
$.each(this, function(k, v) {
alert(k + ' ' + v);
});
});
it works as expected, if you parseJSON the data.
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(function() {
var data = $.parseJSON('{"success":true,"venues":[{"id":237,"title":"Country House Hotel and Restaurant","thumb_image":"thumb_combe-house-buttercups_3617067271.jpg","description":"","subType":1},{"id":579,"title":"Abode Exeter","thumb_image":"wvs.jpg","description":"","subType":0},{"id":484,"title":"Anran Luxury Boutique","thumb_image":"wvs.jpg","description":"","subType":0}]}');
$.each(data.venues, function() {
$.each(this, function(k, v) {
alert(k + ' ' + v);
});
});
});
</script>
</head>
<body>
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments