javascript - 如何在javascript中转换json数据

标签 javascript

我正在尝试使用数据脚本函数转换数据。但我无法在其父数组中添加子项。

var a ={
            "GENERAL_INFORMATION": {
              "value": null,
              "type": "LABEL",
              "editable": false,
              "dataType": null,
              "required": false,
              "displayName": null,
              "pattern": null
            },
            " NUMBER": {
              "value": "9876834940",
              "type": "FIELD",
              "editable": false,
              "dataType": "",
              "required": false,
              "displayName": null,
              "pattern": null
            },
            "CON TYPE": {
              "value": "retailnn_cyn",
              "type": "FIELD",
              "editable": false,
              "dataType": "",
              "required": false,
              "displayName": null,
              "pattern": null
            },
            "User INFORMATION": {
              "value": null,
              "type": "LABEL",
              "editable": false,
              "dataType": null,
              "required": false,
              "displayName": null,
              "pattern": null
            },
            "Title": {
              "value": "Mr",
              "type": "FIELD",
              "editable": true,
              "dataType": "",
              "required": true,
              "displayName": null,
              "pattern": null
            },
            "Gender": {
              "value": "M",
              "type": "FIELD",
              "editable": true,
              "dataType": "",
              "required": true,
              "displayName": null,
              "pattern": null
            },
            "DOB": {
              "value": "23-Oct-1984",
              "type": "FIELD",
              "editable": true,
              "dataType": "DATE",
              "required": true,
              "displayName": null,
              "pattern": null
            }
    };




    var o = [];

    for (var i in a){
      if(a[i].type==='LABEL'){
        a[i].text = i;
        a[i].children = []
        o.push(a[i])
      }else if(a[i].type==='FIELD'){

      }

    }

获取输出;

[{
    "value": null,
    "type": "LABEL",
    "editable": false,
    "dataType": null,
    "required": false,
    "displayName": null,
    "pattern": null,
    "text": "GENERAL_INFORMATION",
    "children": []
}, {
    "value": null,
    "type": "LABEL",
    "editable": false,
    "dataType": null,
    "required": false,
    "displayName": null,
    "pattern": null,
    "text": "User INFORMATION",
    "children": []
}]

预期输出

[{
    "value": null,
    "type": "LABEL",
    "editable": false,
    "dataType": null,
    "required": false,
    "displayName": null,
    "pattern": null,
    "text": "GENERAL_INFORMATION",
    "children": [
      {
          "value": "9876834940",
          "type": "FIELD",
          "editable": false,
          "dataType": "",
          "required": false,
          "displayName": null,
          "pattern": null,
        "text": "NUMBER",
        },
     {
          "value": "retailnn_cyn",
          "type": "FIELD",
          "editable": false,
          "dataType": "",
          "required": false,
          "displayName": null,
          "pattern": null,
       "text": "CON TYPE",
        }
    ]
}, {
    "value": null,
    "type": "LABEL",
    "editable": false,
    "dataType": null,
    "required": false,
    "displayName": null,
    "pattern": null,
    "text": "User INFORMATION",
    "children": [
      {
          "value": "Mr",
          "type": "FIELD",
          "editable": true,
          "dataType": "",
          "required": true,
          "displayName": null,
          "pattern": null,
        "text": "Title",
        },
      {
          "value": "M",
          "type": "FIELD",
          "editable": true,
          "dataType": "",
          "required": true,
          "displayName": null,
          "pattern": null,
          "text": "Gender",
        },
         {
          "value": "23-Oct-1984",
          "type": "FIELD",
          "editable": true,
          "dataType": "",
          "required": true,
          "displayName": null,
          "pattern": null,
          "text": "DOB",
        }
    ]
}]



console.log(JSON.stringify(o))

这是我的代码 https://jsbin.com/wofufuriqe/2/edit?html,js,console

最佳答案

您的代码更新了 a 元素,不确定这是不是一个好主意...

这是“我的解决方案”:(我改了名字 a,o)

const Primo = { 'GENERAL_INFORMATION': 
                  { value      : null
                  , type       : 'LABEL'
                  , editable   : false
                  , dataType   : null
                  , required   : false
                  , displayName: null
                  , pattern    : null
                  } 
              , 'NUMBER': 
                  { value      : '9876834940'
                  , type       : 'FIELD'
                  , editable   : false
                  , dataType   : ''
                  , required   : false
                  , displayName: null
                  , pattern    : null
                  } 
              , 'CON TYPE': 
                  { value      : 'retailnn_cyn'
                  , type       : 'FIELD'
                  , editable   : false
                  , dataType   : ''
                  , required   : false
                  , displayName: null
                  , pattern    : null
                  } 
              , 'User INFORMATION': 
                  { value      : null
                  , type       : 'LABEL'
                  , editable   : false
                  , dataType   : null
                  , required   : false
                  , displayName: null
                  , pattern    : null
                  } 
              , 'Title': 
                  { value      : 'Mr'
                  , type       : 'FIELD'
                  , editable   : true
                  , dataType   : ''
                  , required   : true
                  , displayName: null
                  , pattern    : null
                  } 
              , 'Gender': 
                  { value      : 'M'
                  , type       : 'FIELD'
                  , editable   : true
                  , dataType   : ''
                  , required   : true
                  , displayName: null
                  , pattern    : null
                  } 
              , 'DOB': 
                  { value      : '23-Oct-1984'
                  , type       : 'FIELD'
                  , editable   : true
                  , dataType   : 'DATE'
                  , required   : true
                  , displayName: null
                  , pattern    : null
                  } 
              } 
let res = []
  , Elm = null

for (let item in Primo)
  {
  let e1  = Object.assign({}, Primo[item])
  e1.text = item
  if (e1.type === 'LABEL')
    {
    Elm         = e1
    e1.children = []
    res.push(e1)
    }
  else if(e1.type === 'FIELD')
    {
    Elm.children.push(e1)
    }
  }
  
console.log( res )

关于javascript - 如何在javascript中转换json数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57863472/

相关文章:

javascript - localstorage 值在页面刷新时更改

javascript - CasperJS 和警告框

javascript - 嵌入了 css 皮肤的 tumblr javascript

javascript - HTML - 从 TextArea 获取输入时换行

JavaScript:带有 Promise 的递归函数,解析返回 "undefined"

javascript - 为什么 messageReactionAdd 什么都不做 discord.js

javascript - 在javascript中计算后生成的触发点击事件超链接

javascript - 如何格式化多系列列 2D Fusion Chart 工具提示字符串

javascript - 如何使用JQuery显示相应的div并隐藏其他div?

Javascript:回调函数是唯一允许以前瞻性方式引用外部变量的函数吗?