使用JavaScript解析JSON树

时间:2022-11-04 14:55:08

I want to parse this JSON tree on the basis of the 'Name' attribute and return the matched node/nodes as an array of objects.

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

Kindly paste the JSON tree in here - https://jsonformatter.curiousconcept.com/ to get a better visualization of it.

请将JSON树粘贴到这里——https://jsonformatter.curiousconcept.com/,以便更好地可视化它。

The scenario would be such that, if the user enters 'Rule', all nodes that contain 'Rule*' corresponding to the 'Name' attribute would be returned.

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

To elaborate, the match would be such that if (object.Name.includes('Rule')) it would be valid match.

需要说明的是,如果匹配(object. name .include ('Rule'))是有效的匹配。

Since the JSON tree is huge and has children embedded within children, I was using Defiant.js and the function was built like this -

由于JSON树很大,并且在子树中嵌入了子树,所以我使用了rebel。函数是这样构建的

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

Since DefiantJS does not work on Microsoft's Edge Browser, switching to a compalitibility mode like IE-10 makes DefiantJS work but is breaking few other things. So, I had to rule out DefiantJS.

因为勘误表不能在微软的Edge浏览器上运行,所以切换到IE-10之类的可压缩模式可以使勘误表运行,但它几乎不会破坏其他东西。所以,我必须排除去挑战。

Is another JSON parsing library available to help me out or a JavaScript or jQuery solution which can do it me ?

是否有另一个JSON解析库可以帮助我,或者是一个JavaScript或jQuery解决方案,我可以这样做?

1 个解决方案

#1


1  

You could use an iterative and recursive approach by checking the types of the items and iterate accordingly.

您可以使用迭代和递归方法来检查项目的类型并进行相应的迭代。

This proposal uses a callback for checking the object and return the actual object if condition is met.

这个建议使用回调来检查对象,如果条件满足,则返回实际的对象。

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

#1


1  

You could use an iterative and recursive approach by checking the types of the items and iterate accordingly.

您可以使用迭代和递归方法来检查项目的类型并进行相应的迭代。

This proposal uses a callback for checking the object and return the actual object if condition is met.

这个建议使用回调来检查对象,如果条件满足,则返回实际的对象。

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