Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过jQuery将JSON对象转换为格式化数组_Javascript_Jquery_Json_Twitter Bootstrap_Treeview - Fatal编程技术网

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

Javascript 通过jQuery将JSON对象转换为格式化数组,javascript,jquery,json,twitter-bootstrap,treeview,Javascript,Jquery,Json,Twitter Bootstrap,Treeview,我有一个JSON对象,我想将其转换为数组,以便将其与jQuery插件一起使用。为此,我需要JSON对象采用以下格式: var tree = [ { text: "Parent 1", nodes: [ { text: "Child 1", nodes: [ { text: "Grandchild 1" }, { text: "Grandchild 2"

我有一个JSON对象,我想将其转换为数组,以便将其与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对象,但我不知道如何迭代。

for(树中的i){
console.log(树[i].text);//打印“父1到5”
}
希望对您有所帮助

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

你能给我看一下Json对象格式吗?{“SectionsData”:{.data.rel.ro:{“Characteristics”:“.jcr:{“Characteristics”:““Entropy”:0}}}”,NumberOfSections:29,“SHA1”:“7ED11BBA8EF27B8FE8617F5446142A3A0613CC41”,“Entropy”:6.06652,“CodeSection:{“Characteristics”:“Entropy”:6.19754}我不需要打印父母,我需要获取一个json对象并将其转换为这种格式。
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);