无限级修改JSON里边的key

Posted wgy0528

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无限级修改JSON里边的key相关的知识,希望对你有一定的参考价值。

var tree = [
  "PositionGuid": "f5af03a9-968c-46b1-8b5d-17d8a64be22d",
  "TemplateGuid": "e5d53c43-a77b-419d-8b49-4d32deff14fd",
  "DevNameID": "0010d1d6-2fc1-471c-a82a-3ddb5248de2b",
  "DevName": "AXIS编码器",
  "Parent": "eee8aba0-d642-4e17-89d6-19951e10e042",
  "DevLevel": 2,
  "Children": [
    "PositionGuid": "b849c03f-7cdd-4ebf-bf12-109da72f867e",
    "TemplateGuid": "e5d53c43-a77b-419d-8b49-4d32deff14fd",
    "DevNameID": "11",
    "DevName": "配电柜",
    "Parent": "f5af03a9-968c-46b1-8b5d-17d8a64be22d",
    "DevLevel": 3,
  "Children": [
    "PositionGuid": "ef8da101-a2c9-4d94-b109-4381dc7bf753",
    "TemplateGuid": "e5d53c43-a77b-419d-8b49-4d32deff14fd",
    "DevNameID": "12",
    "DevName": "配电箱",
    "Parent": "b849c03f-7cdd-4ebf-bf12-109da72f867e",
    "DevLevel": 4,
  "Children": [
    "PositionGuid": "f6cfbb8e-97d6-4424-9abd-3d1c85c53bd3",
    "TemplateGuid": "e5d53c43-a77b-419d-8b49-4d32deff14fd",
    "DevNameID": "123",
    "DevName": "气象站",
    "Parent": "ef8da101-a2c9-4d94-b109-4381dc7bf753",
    "DevLevel": 5,
    "Children": []
    ]
  ]
 ,
  "PositionGuid": "c85a24f4-0f7e-47e2-bfb6-f3f761eb3e55",
  "TemplateGuid": "e5d53c43-a77b-419d-8b49-4d32deff14fd",
  "DevNameID": "0010d1d6-2fc1-471c-a82a-3ddb5248de2c",
  "DevName": "UPS电源",
  "Parent": "f5af03a9-968c-46b1-8b5d-17d8a64be22d",
  "DevLevel": 3,
  "Children": [
    "PositionGuid": "5caa3051-3e7b-4063-80e5-34612f00d1f5",
    "TemplateGuid": "e5d53c43-a77b-419d-8b49-4d32deff14fd",
    "DevNameID": "0010d1d6-2fc1-471c-a82a-3ddb5248de2e",
    "DevName": "安防摄像机",
    "Parent": "c85a24f4-0f7e-47e2-bfb6-f3f761eb3e55",
    "DevLevel": 4,
    "Children": [
    "PositionGuid": "db81bf26-f15a-48ca-a12a-8a954299b637",
    "TemplateGuid": "e5d53c43-a77b-419d-8b49-4d32deff14fd",
    "DevNameID": "1",
    "DevName": "安防线缆",
    "Parent": "5caa3051-3e7b-4063-80e5-34612f00d1f5",
    "DevLevel": 5,
    "Children": []
  ]
  ]
  ]
];
//先定好修改的key
var key = ‘children‘
//arr 接受的json
function parseJson(arr)
  arr = arr.slice()
  function toParse(arr)
    console.log(arr,‘====‘);
    arr.forEach(function (item)
    if (item.Children && Array.isArray(item.Children))
      item[key] = item.Children
      toParse(item[key])
    
    delete item.Children  
  )
  return arr
  
  return toParse(arr)
console.log(parseJson(tree))

以上是关于无限级修改JSON里边的key的主要内容,如果未能解决你的问题,请参考以下文章

java修改.json文件:runtime.json的文件; 如何取出captchaCount:300里边的300,并且把300修改掉;如下

python如何修改JSON文件中的某个key值?

jquery无限级下拉框

无限级分类批量导入csv-记录

可能会导致循环或多个级联路径。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束

JSON数据格式生成无限级树结构