jQuery - 通过JSON数组搜索 [英] jQuery - Search through JSON array

查看:119
本文介绍了jQuery - 通过JSON数组搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要搜索JSON数据并找到每个包含kind播放列表的对象,然后遍历轨道并提取将放入hmtl列表的信息。问题是,在某些情况下(取决于URL的类型)而不是包含所有对象的多维数组,json信息只是一个单一对象。



是两种url类型。

这是一个只包含一级信息的播放列表。
http://api.soundcloud.com/playlists/1980151.json ?client_id = bcc776e7aa65dbc29c40ff21a1a94ecd



这是一个只包含多个播放列表作为对象的数组。
http://api.soundcloud.com/users/dubstep /playlists.json?client_id=bcc776e7aa65dbc29c40ff21a1a94ecd



目前的代码是这样的:

  $ .getJSON('http://api.soundcloud.com/users/dubstep/playlists.json?client_id=bcc776e7aa65dbc29c40ff21a1a94ecd',{get_param:'value'},function(url_data){ 
$ .each(url_data,function(i,elem){
if(elem.kind ==='playlist'){
$ .each(elem.tracks,function(i, elem){
console.log(elem.title);
});
}
});
$ b $ p
$ b

仅当 处理用户网址时才起作用有多个播放列表。



为了总结我的问题,我需要一种方法来搜索数组的所有级别以找到具有===播放列表的级别。

解决方案

这应该做你想要的。

  $。each(json,function(i,elem){
if(elem.kind ==='playlist'){
$ .each(elem.tracks,function i,elem){
console.log(elem.title);
});
}
});

更新:

这将适用于任一网址。另外,这里有更多更高级的客户端输出: 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);
}
};


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 hmtl list. The problem is that in some instances (depending on the type of URL) instead of a multidimensional array containing all the objects, the json information is just a singular object.

Below are the two url types.

This is a playlist which contains just one level of information. http://api.soundcloud.com/playlists/1980151.json?client_id=bcc776e7aa65dbc29c40ff21a1a94ecd

This is an array which contains just multiple playlists as objects. http://api.soundcloud.com/users/dubstep/playlists.json?client_id=bcc776e7aa65dbc29c40ff21a1a94ecd

The current code I have is this:

$.getJSON('http://api.soundcloud.com/users/dubstep/playlists.json?client_id=bcc776e7aa65dbc29c40ff21a1a94ecd', { get_param: 'value' }, function(url_data) {
$.each(url_data, function (i, elem) {
    if (elem.kind === 'playlist') {
        $.each(elem.tracks, function (i, elem) {
            console.log(elem.title);
        });
    }
});

});

It only works when dealing with "user" urls where there are multiple playlists.

To sum up what my issue is, I need a way to search through all the levels of an array to find levels with the kind === playlist.

解决方案

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);
    }
};

这篇关于jQuery - 通过JSON数组搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆