简体   繁体   English

Javascript 嵌套 JSON 解析

[英]Javascript Nested JSON Parsing

I have a json object that object contains main nodes and nested nodes.我有一个 json 对象,该对象包含主节点和嵌套节点。 Each node have a property "type", i want to remove the nodes object which contains the property "type = doc".Please find below example json image format.每个节点都有一个属性“type”,我想删除包含属性“type = doc”的节点对象。请在下面找到示例 json 图像格式。 I attached 2 files one image is the input format and other one is the output format.我附上了 2 个文件,一个图像是输入格式,另一个是输出格式。

var json =  {
"navigations": [
{
  "disp_order": "1",
  "menu_id": "25266",
  "nodes": [
    {
      "disp_order": "2",
      "menu_id": "18951",
      "type": "DOC"
    }
  ],
  "type": "MENU"
},
{
  "disp_order": "20",
  "menu_id": "25204",
  "nodes": [
    {
      "disp_order": "1",
      "menu_id": "10295",
      "type": "DOC"
    },
    {
      "disp_order": "10",
      "menu_id": "25207",
      "nodes": [
        {
          "disp_order": "999",
          "menu_id": "17250",
          "type": "DOC"
        },

      ],
      "type": "MENU"
    },
    {
      "disp_order": "20",
      "menu_id": "25209",
      "nodes": [
        {
          "disp_order": "999",
          "menu_id": "18881",
          "type": "DOC"
        },

      ],
      "type": "MENU"
    },
  ],
  "type": "MENU"
},

]
}

输入 Json 格式

输出 Json 格式

https://jsfiddle.net/1hoctvdp/ https://jsfiddle.net/1hoctvdp/

function deleteNonMenu(obj) {
  if (obj.type == "DOC") {
    return true;
  }
  if (obj.nodes) {
    for (var i = 0; i < obj.nodes.length; i++) {
      var res = deleteNonMenu(obj.nodes[i]);
      if (res == true) {
        delete obj.nodes[i];
      }
    }
  }
  return false;
}

for (var i = 0; i < json.navigations.length; i++) {
  var result = deleteNonMenu(json.navigations[i]);
  if (result == true) {
    delete json.navigations[i];
  }
}
console.log(json);

Just as an alternative, you could use this ES6 function, which leaves the original object immutable, and creates the filtered version as return value:作为替代方案,您可以使用这个 ES6 函数,它使原始对象保持不变,并创建过滤版本作为返回值:

 function withoutDocNodes(obj) { return Object(obj) !== obj ? obj // Primitive value: return w/o change : Object.assign(Array.isArray(obj) ? [] : {}, // Create array or object // With following properties: ...Object.keys(obj) // For each property: // Exclude those with DOC type in their value .filter( key => Object(obj[key]).type !== 'DOC') // Get recursive value (also without DOC types) .map( key => [withoutDocNodes(obj[key]), key] ) // Exclude if resulting value is an empty (object or array) .filter ( ([o]) => Object.keys(o).length ) // Produce key/value to add: arrays get index as property .map( ([o, key], i) => ({ [Array.isArray(obj) ? i : key]: o }) ) ); } const obj = { "navigations": [ { "disp_order": "1", "menu_id": "25266", "nodes": [ { "disp_order": "2", "menu_id": "18951", "type": "DOC" } ], "type": "MENU" }, { "disp_order": "20", "menu_id": "25204", "nodes": [ { "disp_order": "1", "menu_id": "10295", "type": "DOC" }, { "disp_order": "10", "menu_id": "25207", "nodes": [ { "disp_order": "999", "menu_id": "17250", "type": "DOC" }, ], "type": "MENU" }, { "disp_order": "20", "menu_id": "25209", "nodes": [ { "disp_order": "999", "menu_id": "18881", "type": "DOC" }, ], "type": "MENU" }, ], "type": "MENU" }, ] }; const result = withoutDocNodes(obj); console.log(result);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

This one is also working, try this code :这个也有效,试试这个代码:

function mainFunction() {
  var data = new Array();
  data = excludeDocs(json.navigations);
}

function excludeDocs(nodes) { 
    var _docs = new Array(); 
    $.each(nodes, function(index, node) { 
      if(typeof node === 'object') { 
          if(node.type === 'DOC') {
              _docs.push(node.menu_id); 
           } 
           else if(typeof node.nodes === 'object') { 
               var _nodes = excludeDocs(node.nodes); 
                if(!(typeof nodes === 'object' && nodes.length > 0)) { 
                     delete node.nodes; 
                } 
                else { 
                   node.nodes = _nodes; 
                } 
            } 
      } 
  }); 
  return nodes.filter(function(n) { 
      return !_docs.includes(n.menu_id); 
  }); 
}

Here is a solution using object-scan .这是使用object-scan的解决方案。 It's powerful for data processing once you wrap your head around it.一旦你把头环绕在它周围,它对于数据处理来说是强大的。

Note: (1) Expects well behaved input and (2) modifies the input in place注意: (1)期望表现良好的输入和(2)就地修改输入

 // const objectScan = require('object-scan'); const prune = (type, input) => { objectScan(['**.nodes[*].type'], { filterFn: ({ value, gparent, gproperty }) => { if (value === type) { gparent.splice(gproperty, 1); } } })(input); }; const json = { navigations: [{ disp_order: '1', menu_id: '25266', nodes: [{ disp_order: '2', menu_id: '18951', type: 'DOC' }], type: 'MENU' }, { disp_order: '20', menu_id: '25204', nodes: [{ disp_order: '1', menu_id: '10295', type: 'DOC' }, { disp_order: '10', menu_id: '25207', nodes: [{ disp_order: '999', menu_id: '17250', type: 'DOC' }], type: 'MENU' }, { disp_order: '20', menu_id: '25209', nodes: [{ disp_order: '999', menu_id: '18881', type: 'DOC' }], type: 'MENU' }], type: 'MENU' }] }; prune('DOC', json); console.log(json); // => { navigations: [ { disp_order: '1', menu_id: '25266', nodes: [], type: 'MENU' }, { disp_order: '20', menu_id: '25204', nodes: [ { disp_order: '10', menu_id: '25207', nodes: [], type: 'MENU' }, { disp_order: '20', menu_id: '25209', nodes: [], type: 'MENU' } ], type: 'MENU' } ] }
 .as-console-wrapper {max-height: 100% !important; top: 0}
 <script src="https://bundle.run/object-scan@16.0.0"></script>

Disclaimer : I'm the author of object-scan免责声明:我是对象扫描的作者

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM