我想解析this基于 'Name' 属性的 JSON 树,并将匹配的节点作为对象数组返回。
请在此处粘贴 JSON 树 - https://jsonformatter.curiousconcept.com/ 以获得更好的可视化效果。
场景是这样的,如果用户输入“Rule”,将返回包含与“Name”属性对应的“Rule*”的所有节点。
详细来说,匹配将是 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 解决方案可以帮我解决这个问题?
最佳答案
您可以通过检查项目的类型并相应地进行迭代来使用迭代和递归方法。
该提议使用回调来检查对象并在满足条件时返回实际对象。
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; }
关于javascript - 使用 JavaScript 解析 JSON 树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41846720/