【问题标题】:Converting a JSON object to a formatted array via jQuery通过 jQuery 将 JSON 对象转换为格式化数组
【发布时间】:2015-05-10 08:35:58
【问题描述】:

我有一个 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 对象,但我不知道如何。

【问题讨论】:

  • 你能告诉我Json对象格式吗?
  • {"SectionsData":{".data.rel.ro":{"Characteristics":"","Entropy":1.55288},".jcr":{"Characteristics":"" ,"Entropy":0}},"NumberOfSections":29,"SHA1":"7ED11BBA8EF27B8FE8617F5446142A3A0613CC41","Entropy":6.06652,"CodeSection":{"Characteristics":"","Entropy":6.19754}}跨度>

标签: javascript jquery json twitter-bootstrap treeview


【解决方案1】:
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);

【讨论】:

    【解决方案2】:

    希望对您有所帮助!

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

      • 我不需要打印父母,我需要取一个json对象并将其转换为这种格式。
      猜你喜欢
      • 2020-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-28
      • 1970-01-01
      • 2018-03-11
      相关资源
      最近更新 更多