1

我必须将 JSON 转换为以下格式,我在将其转换回来时遇到问题。

这是当前格式

[{
    "id": "5",
    "parentid": "0",
    "text": "Device Guides",
    "index": 0,
    "children": [{
        "id": "10",
        "text": "Grandstream GXP-21XX",
        "index": 0
    }, {
        "id": "11",
        "text": "Polycom Soundstation/Soundpoint",
        "index": 1
    }, {
        "id": "23",
        "text": "New Polycom",
        "index": 2
    }]
}, {
    "id": "6",
    "parentid": "0",
    "text": "Pre-Sales Evaluation",
    "index": 1,
    "children": []
}, {
    "id": "7",
    "parentid": "0",
    "text": "Router Setup Guides",
    "index": 2,
    "children": [{
        "id": "9",
        "text": "Sonicwall",
        "index": 0
    }, {
        "id": "12",
        "text": "Cisco",
        "index": 1
    }]
}, {
    "id": "9",
    "parentid": "7",
    "text": "Sonicwall",
    "index": 3,
    "children": []
}, {
    "id": "10",
    "parentid": "5",
    "text": "Grandstream GXP-21XX",
    "index": 4,
    "children": []
}, {
    "id": "11",
    "parentid": "5",
    "text": "Polycom Soundstation/Soundpoint",
    "index": 5,
    "children": []
}, {
    "id": "12",
    "parentid": "7",
    "text": "Cisco",
    "index": 6,
    "children": []
}]

这是我需要的格式:

[{
    "id": "5",
    "parentid": "0",
    "text": "Device Guides",
    "index": "0"
}, {
    "id": "6",
    "parentid": "0",
    "text": "Pre-Sales Evaluation",
    "index": "0"
}, {
    "id": "7",
    "parentid": "0",
    "text": "Router Setup Guides",
    "index": "0"
}, {
    "id": "9",
    "parentid": "7",
    "text": "Sonicwall",
    "index": "0"
}, {
    "id": "10",
    "parentid": "5",
    "text": "Grandstream GXP-21XX",
    "index": "0"
}, {
    "id": "11",
    "parentid": "5",
    "text": "Polycom Soundstation\/Soundpoint",
    "index": "0"
}, {
    "id": "12",
    "parentid": "7",
    "text": "Cisco",
    "index": "0"
}]

基本上,我必须为我正在使用的脚本嵌套它,但服务器希望看到它变平,在当前格式中,第 3 个对象维度以“children”开头。我需要取消嵌套孩子并保持对象按照我需要的格式运行。

4

1 回答 1

3

第一个解决方案,授予您不希望根据 id 对结果数组进行排序:

function visitor(graph) {
  var i, l,
  nodes=[],
  visited=[];

  function clone(n) {
     // improve the function yourself I'm lazy
     var i,l,
         props=["id","parentid","index","text"],
         result={};
     for (i = 0, l = props.length; i < l; i++) { 
        if (n[props[i]]) {
          result[props[i]]= n[props[i]];
        }
     }
     return result;
  }

  function helper (node) {
    var i, limit;
    if (visited.indexOf(node.id) == -1) {
      visited.push(node.id);
      nodes.push(clone(node));
      if( node.children) {
        for (i = 0, limit = node.children.length; i < limit; i++) {
          helper(node.children[i]);
        }
      }
    }
  }

  for (i = 0, l = graph.length; i < l; i++) {
    helper(graph[i]);
  }

  return nodes;
}

var graph =     [{
    "id": "5",
    "parentid": "0",
    "text": "Device Guides",
    "index": 0,
    "children": [{
        "id": "10",
        "text": "Grandstream GXP-21XX",
        "index": 0
    }, {
        "id": "11",
        "text": "Polycom Soundstation/Soundpoint",
        "index": 1
    }, {
        "id": "23",
        "text": "New Polycom",
        "index": 2
    }]
}, {
    "id": "6",
    "parentid": "0",
    "text": "Pre-Sales Evaluation",
    "index": 1,
    "children": []
}, {
    "id": "7",
    "parentid": "0",
    "text": "Router Setup Guides",
    "index": 2,
    "children": [{
        "id": "9",
        "text": "Sonicwall",
        "index": 0
    }, {
        "id": "12",
        "text": "Cisco",
        "index": 1
    }]
}, {
    "id": "9",
    "parentid": "7",
    "text": "Sonicwall",
    "index": 3,
    "children": []
}, {
    "id": "10",
    "parentid": "5",
    "text": "Grandstream GXP-21XX",
    "index": 4,
    "children": []
}, {
    "id": "11",
    "parentid": "5",
    "text": "Polycom Soundstation/Soundpoint",
    "index": 5,
    "children": []
}, {
    "id": "12",
    "parentid": "7",
    "text": "Cisco",
    "index": 6,
    "children": []
}];

nodes = visitor(graph);

是的,我知道,辅助函数会传递副作用,但我已将它们限制在访问者函数中以减少伤害,并且还有改进的余地(至少根据 id 对结果数组进行排序),但我会将它们留给你

于 2013-04-13T06:02:20.447 回答