使用JavaScript解析JSON树

3

我希望根据“Name”属性解析 JSON树,并将匹配的节点作为对象数组返回。

请在此处粘贴JSON树- https://jsonformatter.curiousconcept.com/以获得更好的可视化效果。

场景是这样的,如果用户输入“Rule”,则会返回所有包含“Rule *”对应于“Name”属性的节点。

换言之,匹配将是这样的if (object.Name.includes('Rule'))它将是有效的匹配。

由于JSON树庞大且具有嵌套在子代内部的子代,因此我正在使用Defiant.js,并且函数构建如下 -

$scope.doASearch = function(elm) {
        var as = '//*[contains(Name, ' + '"' + elm + '"' + ')]';
        $scope.found = JSON.search($scope.treedata, as);
        $scope.treedata = _.uniq($scope.found, 'MetaCatID');
};

由于DefiantJS在Microsoft的Edge浏览器上无法工作,切换到IE-10等兼容模式可以使DefiantJS工作,但会破坏其他一些功能。因此,我不得不排除DefiantJS。

是否有其他可用的JSON解析库可以帮助我,或者有JavaScript或jQuery解决方案可以代替它?


3
你觉得把JSON树的“最简”版本直接放在问题中,而不是一个链接更好吗?这样一来,即使你已经得到答案,链接也不会失效。请注意,翻译后的内容不能改变原意,也不能添加注释或其他内容。 - Jaromanda X
1
在这里有什么困难?只是一个带有几个if语句的递归函数。 - smnbbrv
@smnbbrv - 你能否用代码来表达你的建议? - CodeWalker
1
请粘贴JSON - 关键不在于这个 ... 关键是这个问题在两周、两个月、两年后是否还能被理解 ... 那个链接那时候还有效吗? - Jaromanda X
该链接已失效... - user3658510
显示剩余7条评论
1个回答

3
你可以使用迭代和递归的方法来检查项目类型并相应地进行迭代。
此提议使用回调函数来检查对象,并在满足条件时返回实际对象。

function search(array, fn) {
    var result = [];
    array.forEach(function iter(o) {
        if (!o || typeof o !== 'object') {
            return;
        }
        if (fn(o)) {
            result.push(o);
            return;
        }
        Object.keys(o).forEach(function (k) {
            iter(o[k]);
        });
    });
    return result;
}

var data = [{ tuple: { old: { MetaCategory: { MetaCatID: 517, ParentMetaCatRef: 0, Name: "D Application" } } }, MetaCatID: 517, ParentMetaCatRef: 0, Name: "D Application", subCategories: [{ tuple: { old: { MetaCategory: { MetaCatID: 518, ParentMetaCatRef: 517, Name: "Compass" } } }, MetaCatID: 518, ParentMetaCatRef: 517, Name: "Compass" }, { tuple: { old: { MetaCategory: { MetaCatID: 519, ParentMetaCatRef: 517, Name: "Orbe" } } }, MetaCatID: 519, ParentMetaCatRef: 517, Name: "Orbe" }, { tuple: { old: { MetaCategory: { MetaCatID: 520, ParentMetaCatRef: 517, Name: "PSI" } } }, MetaCatID: 520, ParentMetaCatRef: 517, Name: "PSI" }, { tuple: { old: { MetaCategory: { MetaCatID: 521, ParentMetaCatRef: 517, Name: "SAP" } } }, MetaCatID: 521, ParentMetaCatRef: 517, Name: "SAP" }] }, { tuple: { old: { MetaCategory: { MetaCatID: 541, ParentMetaCatRef: 0, Name: "D Versions" } } }, MetaCatID: 541, ParentMetaCatRef: 0, Name: "D Versions", subCategories: [{ tuple: { old: { MetaCategory: { MetaCatID: 542, ParentMetaCatRef: 541, Name: "Baseline 2016-12-31" } } }, MetaCatID: 542, ParentMetaCatRef: 541, Name: "Baseline 2016-12-31" }, { tuple: { old: { MetaCategory: { MetaCatID: 543, ParentMetaCatRef: 541, Name: "CLS step 3 2017-04-15" } } }, MetaCatID: 543, ParentMetaCatRef: 541, Name: "CLS step 3 2017-04-15" }] }, { tuple: { old: { MetaCategory: { MetaCatID: 365, ParentMetaCatRef: 0, Name: "Market" } } }, MetaCatID: 365, ParentMetaCatRef: 0, Name: "Market", subCategories: [{ tuple: { old: { MetaCategory: { MetaCatID: 366, ParentMetaCatRef: 365, Name: "Sector" } } }, MetaCatID: 366, ParentMetaCatRef: 365, Name: "Sector", subCategories: [{ tuple: { old: { MetaCategory: { MetaCatID: 463, ParentMetaCatRef: 366, Name: "term" } } }, MetaCatID: 463, ParentMetaCatRef: 366, Name: "term" }, { tuple: { old: { MetaCategory: { MetaCatID: 464, ParentMetaCatRef: 366, Name: "category" } } }, MetaCatID: 464, ParentMetaCatRef: 366, Name: "category" }, { tuple: { old: { MetaCategory: { MetaCatID: 367, ParentMetaCatRef: 366, Name: "Subsector" } } }, MetaCatID: 367, ParentMetaCatRef: 366, Name: "Subsector" }] }] }];

console.log(search(data, function (o) { return o.MetaCatID > 500; }));
console.log(search(data, function (o) { return o.Name && o.Name.includes('P'); }));
.as-console-wrapper { max-height: 100% !important; top: 0; }


当我使用String.contains或String.includes时,它无法正常工作。 请看这里-https://jsbin.com/cuwocupelo/edit?html,js,console - CodeWalker
也许你的浏览器不支持这些方法。我应该在jsbin上查看吗? - Nina Scholz
你需要先检查属性。我稍微改了一下函数。 - Nina Scholz
问题是由于某些情况下属性变成了undefined,因此我像这样修复它 - if(o.Name !== undefined) return (o.Name.indexOf('PS') >= 0);。由于Internet Explorer或Edge不支持String.containsString.includes方法,所以我必须使用indexOf - CodeWalker

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接