如何在javascript中转换json数据
我试图使用datascript函数转换数据。但我无法在其父数组中添加子数组如何在javascript中转换json数据,javascript,Javascript,我试图使用datascript函数转换数据。但我无法在其父数组中添加子数组 var a ={ "GENERAL_INFORMATION": { "value": null, "type": "LABEL", "editable": false, "dataType": null, "required": false,
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))
这是我的密码
您提供的代码当前无法处理向父数据添加子数据的问题 您需要有一些方法来引用父对象以将子对象添加到其中,因此您可以做的一件事是在推送到数组之前构建它
var o = [];
var parent = {}
for (var i in a){
if(a[i].type==='LABEL'){
if (parent.hasOwnProperty('children')) o.push(parent)
parent = a[i]
parent.text = i;
parent.children = []
}else if(a[i].type==='FIELD'){
parent.children.push(a[i])
}
}
o.push(parent)
console.log(o)
这在很大程度上依赖于
a
中的数据,这些数据总是以标签对象的形式出现,后跟相关的字段对象。它非常脆弱,但这是修复代码的最简单方法,同时保留最初的操作 您的代码更新a
元素,不确定它是否是上帝的想法
这是“我的解决方案”:(我更改了名称a、o)
const Primo={“一般信息”:
{值:null
,键入:“标签”
,可编辑:false
,数据类型:null
,必填项:false
,displayName:null
,模式:空
}
,“编号”:
{值:'9876834940'
,键入:“字段”
,可编辑:false
,数据类型:“”
,必填项:false
,displayName:null
,模式:空
}
,“CON类型”:
{值:'retailnn_cyn'
,键入:“字段”
,可编辑:false
,数据类型:“”
,必填项:false
,displayName:null
,模式:空
}
,“用户信息”:
{值:null
,键入:“标签”
,可编辑:false
,数据类型:null
,必填项:false
,displayName:null
,模式:空
}
“标题”:
{值:'Mr'
,键入:“字段”
,可编辑:true
,数据类型:“”
,必填项:true
,displayName:null
,模式:空
}
“性别”:
{值:'M'
,键入:“字段”
,可编辑:true
,数据类型:“”
,必填项:true
,displayName:null
,模式:空
}
,“DOB”:
{价值:'1984年10月23日'
,键入:“字段”
,可编辑:true
,数据类型:“日期”
,必填项:true
,displayName:null
,模式:空
}
}
让res=[]
,Elm=null
用于(以原样输入项目)
{
设e1=Object.assign({},Primo[item])
e1.text=项目
如果(e1.type==='LABEL')
{
Elm=e1
e1.children=[]
res.push(e1)
}
else if(e1.type==='FIELD')
{
榆树。儿童。推(e1)
}
}
console.log(res)
我要说的第一件事是,当您开始处理这些对象集时,不能保证它们是按此顺序排列的。我强烈建议向类型为“FIELD”的对象添加属性,以标识其父对象。
假设您已将field“parentKey”
添加到所有指定其父对象的“field”对象中。例如:parentKey:“一般信息”
然后您的代码将如下所示
let a = {
"GENERAL_INFORMATION": {
"value": null,
"type": "LABEL",
"editable": false,
"dataType": null,
"required": false,
"displayName": null,
"pattern": null
},
" NUMBER": {
"parentKey": "GENERAL_INFORMATION",
"value": "9876834940",
"type": "FIELD",
"editable": false,
"dataType": "",
"required": false,
"displayName": null,
"pattern": null
},
"CON TYPE": {
"parentKey": "GENERAL_INFORMATION",
"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": {
"parentKey": "User INFORMATION",
"value": "Mr",
"type": "FIELD",
"editable": true,
"dataType": "",
"required": true,
"displayName": null,
"pattern": null
},
"Gender": {
"parentKey": "User INFORMATION",
"value": "M",
"type": "FIELD",
"editable": true,
"dataType": "",
"required": true,
"displayName": null,
"pattern": null
},
"DOB": {
"parentKey": "User INFORMATION",
"value": "23-Oct-1984",
"type": "FIELD",
"editable": true,
"dataType": "DATE",
"required": true,
"displayName": null,
"pattern": null
}
};
let o = [];
// Modifying the object as you prefer
for (let i in a) {
if (!a.hasOwnProperty(i)) continue;
if (a[i].type === 'LABEL') {
a[i].text = i;
} else {
let parentObj = a[a[i].parentKey];
parentObj.children = parentObj.children ? parentObj.children : [];
parentObj.children.push(a[i]);
}
}
// Push parent objects to array
for (let i in a) {
if (!a.hasOwnProperty(i)) continue;
if (a[i].type === 'LABEL') {
o.push(a[i]);
}
}
console.log(JSON.stringify(o));
通过这种方式,您可以保证100%获得所需的输出。没有将项目映射到子元素的共同点,我所能推断的是,
type:'FIELD'
)后面的直接元素(type:'LABEL'
应该成为前一个type:'LABEL'
元素的子元素。是这样吗?孩子们的外键是什么,或者创建他们所需要的逻辑是什么?还有,“数据脚本函数”是什么意思?@gͧtͯcͭlͪiͯtͯcͭȟ是的,正确。。!!