通过jQuery将JSON对象转换为格式化数组

时间:2015-03-09 08:28:52

标签: javascript jquery json twitter-bootstrap treeview

我有一个JSON对象,我想将其转换为数组,以便我可以将它与bootstrap-treeview jQuery插件一起使用。为此,我需要JSON对象采用以下格式:

var tree = [
{
  text: "Parent 1",
  nodes: [
    {
      text: "Child 1",
      nodes: [
        {
          text: "Grandchild 1"
        },
        {
          text: "Grandchild 2"
        }
      ]
    },
  {
    text: "Child 2"
  }
]
},
{
  text: "Parent 2"
},
{
  text: "Parent 3"
},
{
  text: "Parent 4"
},
{
  text: "Parent 5"
}
];

我知道我可能需要递归迭代JSON对象,但我不知道如何。

3 个答案:

答案 0 :(得分:1)

希望这有帮助!

    arrConvert = new Array();
    function convertItem(p){
        arrItem = new Array();
        for (var key in p) {
              if (p.hasOwnProperty(key)) {
                  if($.type(p[key]) === "string" || $.type(p[key]) === "number" )
                      arrItem.push({"text":p[key]});
                  else{
                      arrItem.push({"nodes":convertItem(p[key])});
                  }
              }
        }
        return arrItem;
    }

    $(function(){
        var testArr = {
                        "SectionsData":[
                                    {"Characteristics":"abc", "Entropy":1.55288}, 
                                    {"Characteristics":"xyz",   "Entropy":1.55288},
                                    {"Characteristics":"mno", "Entropy":1.55288}
                                ],
                        "NumberOfSections":29,
                        "SHA1":"7ED11BBA8EF27B8‌​FE8617F5446142A3A0613CC41",
                        "Entropy":6.06652,
                        "CodeSection":[
                                        {"Characteristics":"abc1", "Entropy":1.55288}, 
                                        {"Characteristics":"xyz2",  "Entropy":1.55288},
                                        {"Characteristics":"mno3", "Entropy":1.55288}
                                    ]        
                        };

        //This is array after convert
        arrConvert = convertItem(testArr);
    }); 

答案 1 :(得分:0)

for(i in tree) {
  console.log(tree[i].text);  //print "Parent 1 to 5"
}

参考:http://www.w3schools.com/js/js_loop_for.asp

答案 2 :(得分:0)

var treeData = [];
var yourData = {
    "SectionsData": {
        ".data.rel.ro": {
            "Characteristics": "",
            "Entropy": 1.55288
        },
        ".jcr": {
            "Characteristics": "",
            "Entropy": 0
        }
    },
    "NumberOfSections": 29,
    "SHA1": "7ED11BBA8EF27B8‌​FE8617F5446142A3A0613CC41",
    "Entropy": 6.06652,
    "CodeSection": {
        "Characteristics": "",
        "Entropy": 6.19754
    }
};


function format(aSource, aTarget) {

    for (propertyName in aSource) {
        if (aSource.hasOwnProperty(propertyName) && aSource[propertyName] instanceof Object) {
            var newNode = {text: propertyName, nodes: []};
            aTarget.push(newNode);
            format(aSource[propertyName], newNode.nodes);
        }
        else{
            aTarget.push({text: propertyName});
        }
    }
}

format(yourData, treeData);

console.log(treeData);