jQuery - Search through JSON array

后端 未结 3 1705
故里飘歌
故里飘歌 2021-02-06 16:52

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

相关标签:
3条回答
  • 2021-02-06 17:51

    try this code:

    var objJson = jQuery.parseJSON(json);
    jQuery.each(objJson , function(i, val) {
          alert(val);
        });
    
    0 讨论(0)
  • 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);
        }
    };
    
    0 讨论(0)
  • 2021-02-06 17:52

    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).

    0 讨论(0)
提交回复
热议问题