-3
var vJson = 
    '{"tpaCo":[{"name":"Audit Company1", \
                         "aud":[{"name":"auditor1","asgn":"1","fnds":"1","lead":"1"}, \
                                {"name":"auditor2","asgn":"1","fnds":"0","lead":"1"}, \
                                {"name":"auditor3","asgn":"0","fnds":"1","lead":"0"}, \
                                {"name":"auditor4","asgn":"1","fnds":"1","lead":"0"}, \
                                {"name":"auditor5","asgn":"1","fnds":"1","lead":"0"}, \
                                {"name":"auditor6","asgn":"0","fnds":"1","lead":"0"}, \
                                {"name":"auditor7","asgn":"1","fnds":"1","lead":"0"}, \
                                {"name":"auditor8","asgn":"1","fnds":"1","lead":"0"}, \
                                {"name":"auditor9","asgn":"0","fnds":"1","lead":"0"}, \
                                {"name":"auditor10","asgn":"1","fnds":"1","lead":"0"}, \
                                {"name":"auditor11","asgn":"1","fnds":"1","lead":"0"}]}, \
                        {"name":"Audit Company2", \
                         "aud":[{"name":"auditor3","asgn":"0","fnds":"0","lead":"1"}, \
                                {"name":"auditor4","asgn":"0","fnds":"0","lead":"0"} \
                               ] \
                        } \
                      ]}';
      var vObj =  JSON.parse(vJson);

有什么方法可以过滤 JSON 对象。例如,我想搜索audit1 * asgn * 值。过滤器可以是动态的,就像它可以是 audit1 或 audit11。我也想知道如何将上面的 json 转换为数组。这将使我的搜索变得容易(以防无法通过直接 JSON 搜索进行搜索)。

4

1 回答 1

0

像这样的东西?

UPD 示例

用法

var jsonfilter = new JSONDataFilter(data);
//jsoonfilter.filter(parentNode, parentNodeValue, childrenNode, childNode, childNodeValue)
jsonfilter.filter('name', 'AuditCo1', 'auds', 'asgn', 1);
jsonfilter.filter('id', '2', 'auds', 'name', 'au2');

输出

[{name:aud3, asgn:1},{name:aud5, asgn:1},{name:aud6, asgn:1}]

JSON数据过滤器

function JSONDataFilter(data){
    _data = {};
    var construct = function(data){
        _data = data;
    }

    this.filter = function(parent, parentValue, childSet, child, childValue){
        var result = [];
        for(var parentAttr in _data){
            if(_data[parentAttr][parent] != parentValue){
                continue;
            }
            var children = _data[parentAttr][childSet];
            for(var childAttr in children){
                if(children[childAttr][child] == childValue){
                    result.push(children[childAttr]);
                }
            }
        }

        return result;
    }
    construct(data);
}
于 2013-08-28T06:07:15.770 回答