JSON对象 - 递归更改所有子项的属性名称

时间:2016-02-02 10:04:28

标签: javascript json

我有以下json对象。

myJsonObj = {  
   "name":"Laptop",
   "type":"hardware computer laptop",
   "forward":[  
      {  
         "name":"Depends On",
         "forward":[  
            {  
               "name":"test asset 1",
               "type":"hardware",
               "link":"somelink"
            }
         ]
      },
      {  
         "name":"somename",
         "forward":[  
            {  
               "name":"test asset 5",
               "type":"hardware",
               "link":"somelink"
            }
         ]
      }
   ],
   "inverse":[  
      {  
         "name":"somename",
         "inverse":[  
            {  
               "name":"test asset 4",
               "ciTypeCls":"hardware",
               "link":"somelink"
            },
            {  
               "name":"test asset 1",
               "ciTypeCls":"hardware",
               "link":"somelink"
            }
         ]
      },
      {  
         "name":"somename",
         "inverse":[  
            {  
               "name":"test asset 1",
               "ciTypeCls":"hardware",
               "link":"somelink"
            }
         ]
      }
   ]
}

我正在尝试将属性名称forward更改为children

以下是我的代码

myJsonObj = {
  "name": "Laptop",
  "type": "hardware computer laptop",
  "forward": [{
    "name": "Depends On",
    "forward": [{
      "name": "test asset 1",
      "type": "hardware",
      "link": "somelink"
    }]
  }, {
    "name": "somename",
    "forward": [{
      "name": "test asset 5",
      "type": "hardware",
      "link": "somelink"
    }]
  }],
  "inverse": [{
    "name": "somename",
    "inverse": [{
      "name": "test asset 4",
      "ciTypeCls": "hardware",
      "link": "somelink"
    }, {
      "name": "test asset 1",
      "ciTypeCls": "hardware",
      "link": "somelink"
    }]
  }, {
    "name": "somename",
    "inverse": [{
      "name": "test asset 1",
      "ciTypeCls": "hardware",
      "link": "somelink"
    }]
  }]
}

jQuery.each(myJsonObj, function(e) {
  e.children = e.forward;
  delete e.forward;
});

console.log(myJsonObj);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

它似乎不起作用,我做错了什么?

3 个答案:

答案 0 :(得分:1)

试试这个:

 jQuery.each(myJsonObj.forward, function(i , item) {
     //Nested forward
     item.children = item.forward;
     delete item.forward;
});
   //PARENT forward
    myJsonObj.children = myJsonObj.forward;
    delete myJsonObj.forward;

JSFiddle https://jsfiddle.net/xbkp10Ly/

答案 1 :(得分:0)

根据您当前的JSON结构,简单的查找和替换可以正常工作。

var myJsonObj = {
  "name": "Laptop",
  "type": "hardware computer laptop",
  "forward": [{
    "name": "Depends On",
    "forward": [{
      "name": "test asset 1",
      "type": "hardware",
      "link": "somelink"
    }]
  }, {
    "name": "somename",
    "forward": [{
      "name": "test asset 5",
      "type": "hardware",
      "link": "somelink"
    }]
  }],
  "inverse": [{
    "name": "somename",
    "inverse": [{
      "name": "test asset 4",
      "ciTypeCls": "hardware",
      "link": "somelink"
    }, {
      "name": "test asset 1",
      "ciTypeCls": "hardware",
      "link": "somelink"
    }]
  }, {
    "name": "somename",
    "inverse": [{
      "name": "test asset 1",
      "ciTypeCls": "hardware",
      "link": "somelink"
    }]
  }]
}

document.write('<pre>' + JSON.stringify(myJsonObj).replace(/forward/g, "children") + '</pre>');

更安全,因为它只检查属性而不是值

JSON.parse(JSON.stringify(myJsonObj).replace(/forward(?=\":)/g,"children"))

选中此link

答案 2 :(得分:0)

这是一个以递归样式迭代对象和数组的提议。

它使用depth-first search,因为实际节点必须保留,直到访问所有从属节点。

var obj = { "name": "Laptop", "type": "hardware computer laptop", "forward": [{ "name": "Depends On", "forward": [{ "name": "test asset 1", "type": "hardware", "link": "somelink" }] }, { "name": "somename", "forward": [{ "name": "test asset 5", "type": "hardware", "link": "somelink" }] }], "inverse": [{ "name": "somename", "inverse": [{ "name": "test asset 4", "ciTypeCls": "hardware", "link": "somelink" }, { "name": "test asset 1", "ciTypeCls": "hardware", "link": "somelink" }] }, { "name": "somename", "inverse": [{ "name": "test asset 1", "ciTypeCls": "hardware", "link": "somelink" }] }] };

function replaceProperty(o, from, to) {

    function f(r, p) {
        if (Array.isArray(r[p])) {
            iterA(r[p]);
        }
        if (typeof r[p] === 'object') {
            iterO(r[p]);
        }
        if (p === from) {
            r[to] = r[from];
            delete r[from];
        }

    }

    function iterO(o) {
        Object.keys(o).forEach(function (k) { f(o, k); });
    }

    function iterA(a) {
        a.forEach(function (_, i, aa) { f(aa, i); });
    }

    iterO(o);
}

replaceProperty(obj, 'forward', 'children');
document.write('<pre>' + JSON.stringify(obj, 0, 4) + '</pre>');