I need to search through JSON data and find every object that contains the \"kind\" \"playlist\" and then go through the tracks and pull information which will be put into a hmt
try this code:
var objJson = jQuery.parseJSON(json);
jQuery.each(objJson , function(i, val) {
alert(val);
});
This should do what you're looking for.
$.each(json, function (i, elem) {
if (elem.kind === 'playlist') {
$.each(elem.tracks, function (i, elem) {
console.log(elem.title);
});
}
});
UPDATE:
This will work with either URL. Also, here's a fiddle with more a more advanced client-side output: http://jsfiddle.net/jTLvE/
var parse = function (json) {
$.each(json, function (i, elem) {
if (elem.kind === 'playlist') {
$.each(elem.tracks, function (i, elem) {
console.log(elem.title);
});
}
});
},
parseReturnedData = function (json) {
var isObject = (typeof json === 'object' && json instanceof Object),
isArray = (typeof json === 'object' && json instanceof Array),
newArray = [];
if (isArray) {
parse(json);
} else if (isObject) {
newArray.push(json);
parse(newArray);
}
};
Have you tried json path? I used it and it's small and fast, especially for searching json trees (for searching and writing I'd use flock though).