我有一个有趣的情况,我试图包围我的头。我可以访问JSON
输出,其中的键名称是_22
,_155
等数字。我创建了一个映射,其中包含所有这些键名称的转换,但我有很难想出如何更新json
并将原始键名替换为地图中的键名。
以下是我的json
字符串的示例,其中包含实际字段名称:
FieldID
对应于源json
数据,没有下划线。
[
{
"FieldID": "7",
"FieldName": "Emp ID",
"IsSortableInput": "0",
"IsFilterableInput": "0",
"ViewAlias": "EmpID",
"LogicOperator": "IN",
"AllowMultipleValues": "1",
"IsViewable": "1",
"PrimaryOnly": "0",
"DisplayOrder": "1"
},
{
"FieldID": "3",
"FieldName": "QID",
"InputTypeID": "1",
"FieldPlaceholder": "Enter Employee Name",
"IsSortableInput": "1",
"IsFilterableInput": "1",
"ViewAlias": "QID",
"LogicOperator": "IN",
"AllowMultipleValues": "1",
"IsViewable": "1",
"PrimaryOnly": "0",
"DisplayOrder": "2",
"FieldTypeDesc": "PeopleSearch"
},
{
"FieldID": "6",
"FieldName": "NTID",
"IsSortableInput": "1",
"IsFilterableInput": "0",
"ViewAlias": "NTID",
"LogicOperator": "IN",
"AllowMultipleValues": "1",
"IsViewable": "1",
"PrimaryOnly": "0",
"DisplayOrder": "3"
}
我的目标是遍历源json
字符串,在地图中找到键名(定义为json)并重命名它们。最终在我的源json数据中用_7
替换Emp ID
。
来源JSON:
[
{
"_3": "Q123",
"_7": "E12345"
},
{
"_3": "X123",
"_7": "E34567"
}
]
Desired Output是改变源JSON,将其转换为:
[
{
"QID": "Q123",
"Emp ID": "E12345"
},
{
"QID": "X123",
"Emp ID": "E34567"
}
]
我很遗憾甚至从这个方法开始。不确定是否可以重命名密钥名称,或者是否需要在流程等中删除密钥名称。
寻找解决此问题的一些建议。
答案 0 :(得分:3)
您可以为旧密钥和新密钥构建引用,并迭代数组和密钥。然后分配给新属性并删除旧属性。
var keynames = [{ FieldID: 7, FieldName: "Emp ID", IsSortableInput: 0, IsFilterableInput: 0, ViewAlias: "EmpID", LogicOperator: "IN", AllowMultipleValues: 1, IsViewable: 1, PrimaryOnly: 0, DisplayOrder: 1 }, { FieldID: 3, FieldName: "QID", InputTypeID: 1, FieldPlaceholder: "Enter Employee Name", IsSortableInput: 1, IsFilterableInput: 1, ViewAlias: "QID", LogicOperator: "IN", AllowMultipleValues: 1, IsViewable: 1, PrimaryOnly: 0, DisplayOrder: 2, FieldTypeDesc: "PeopleSearch" }, { FieldID: 6, FieldName: "NTID", IsSortableInput: 1, IsFilterableInput: 0, ViewAlias: "NTID", LogicOperator: "IN", AllowMultipleValues: 1, IsViewable: 1, PrimaryOnly: 0, DisplayOrder: 3 }],
source = [{ _3: "Q123", _7: "E12345" }, { _3: "X123", _7: "E34567" }],
reference = {};
keynames.forEach(function (o) {
reference['_' + o.FieldID] = o.FieldName;
});
source.forEach(function (o) {
Object.keys(o).forEach(function (k) {
o[reference[k]] = o[k];
delete o[k];
});
});
console.log(source);

.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:1)
解析完JSON文件后,我会使用:
然后删除key.name
重新创建一个具有所需值的键,希望这会有所帮助。
答案 2 :(得分:1)
yum install condor-all
答案 3 :(得分:0)
let mapping = [
{
"FieldID": "7",
"FieldName": "Emp ID",
"IsSortableInput": "0",
"IsFilterableInput": "0",
"ViewAlias": "EmpID",
"LogicOperator": "IN",
"AllowMultipleValues": "1",
"IsViewable": "1",
"PrimaryOnly": "0",
"DisplayOrder": "1"
},
{
"FieldID": "3",
"FieldName": "QID",
"InputTypeID": "1",
"FieldPlaceholder": "Enter Employee Name",
"IsSortableInput": "1",
"IsFilterableInput": "1",
"ViewAlias": "QID",
"LogicOperator": "IN",
"AllowMultipleValues": "1",
"IsViewable": "1",
"PrimaryOnly": "0",
"DisplayOrder": "2",
"FieldTypeDesc": "PeopleSearch"
},
{
"FieldID": "6",
"FieldName": "NTID",
"IsSortableInput": "1",
"IsFilterableInput": "0",
"ViewAlias": "NTID",
"LogicOperator": "IN",
"AllowMultipleValues": "1",
"IsViewable": "1",
"PrimaryOnly": "0",
"DisplayOrder": "3"
}
];
let source = [
{
"_3": "Q123",
"_7": "E12345"
},
{
"_3": "X123",
"_7": "E34567"
}
];
source.map(ele => {
let ret = {};
Object.keys(ele).forEach(key => {
let keyNum = key.replace('_','');
let match = mapping.find(ele => {
return ele.FieldID === keyNum;
});
ret[match.FieldName] = ele[key];
});
return ret;
});