2013-04-13 36 views
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" 
}] 

基本上,我有窝它我使用的脚本但服务器期望看到它变平,在当前格式中,第三个对象维以“children”开始。我需要UNNEST孩子,并保持持续的对象,正如我需要它的格式

+0

仅供参考,JSON是[文本格式](http://en.wikipedia.org/wiki/JSON)。你在这里是JavaScript文字符号或只是一个JavaScript数据结构。这不是JSON。 – jfriend00

+1

请定义“unnest”以获得明智的答案。 – akonsu

+2

这是数组的内部内容吗?这是有效的JS既不是有效的JSON。 – plalx

回答

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排序产生的数组),但我会让他们you

+0

感谢您拯救我的生命。 – 2015-11-20 21:32:40