class Transformation {
constructor() {
this.colHeaders = {
error_description: "Description",
error_status: "Status",
error_code: "Error Code"
};
}
getColHeader() {
return this.colHeaders;
}
}
var jsonData = {
error_description: "Already Rejected",
error_status: "Faliure",
error_code: "401"
};
var clmDetails = new Transformation();
var obj = clmDetails.getColHeader();
var json_conversion = {};
for (var key in jsonData) {
if (jsonData.hasOwnProperty(key)) {
var k = obj[key.toLowerCase().replace(/\s/g, "_")];
var val = jsonData[key];
json_conversion[k] = val;
}
}
console.log(json_conversion);
Input Json:
{
"error_description": "Already Rejected",
"error_status": "Faliure",
"error_code": "401"
}
output Json I am getting:
O/P:
{
"Description": "Already Rejected",
"Status": "Faliure",
"Error Code": "401"
}
但是再次,我需要根据我得到的输出json将其转换为旧的json,为此,我必须将输出json的键与类Transformation中的值进行匹配,然后按如下所示重新构建旧的json。
Expected Output:
{
"error_description": "Already Rejected",
"error_status": "Faliure",
"error_code": "401"
}
下面,我给出了上面用于转换输入到输出json的代码。 但如上所述,我需要再次输出json以输入json。
我必须在相同的Tranformation类中进行操作,需要帮助。 预先感谢。
场景: 休息服务>>> JSON
{
error_description: "Already Rejected",
error_status: "Faliure",
error_code: "401"
}
转换json(因为我无法将字段名称显示为error_description,应该为Description),并在ui中以表格形式显示为可编辑字段 再次编辑数据并将json发送到休息服务
{
"Description": "Already Rejected",
"Status": "Faliure",
"Error Code": "401"
}
但是现在json已转换,因此休息服务将不再需要。所以我再次需要json作为
{
error_description: "Already Rejected",
error_status: "Faliure",
error_code: "401"
}
我无法在Rest服务中添加json转换属性,因为它来自第三方。
答案 0 :(得分:1)
class Transformation {
constructor() {
this.colOutHeaders = {
error_description: "Description",
error_status: "Status",
error_code: "Error Code"
};
this.colInHeaders = {
"Description": "error_description",
"Status": "error_status",
"Error Code": "error_code"
};
}
getOutHeader() {
return this.colOutHeaders;
}
getInHeader() {
return this.colInHeaders;
}
}
function transform(jsonData, obj) {
var json_conversion = {};
for (var key in jsonData) {
if (jsonData.hasOwnProperty(key)) {
var k = obj[key];
var val = jsonData[key];
json_conversion[k] = val;
}
}
return json_conversion;
}
var jsonData = {
error_description: "Already Rejected",
error_status: "Faliure",
error_code: "401"
};
var clmDetails = new Transformation();
var obj = clmDetails.getOutHeader();
output = transform(jsonData, obj);
console.log(output);
revertOutput = transform(output, clmDetails.getInHeader());
console.log(revertOutput);
我已更新,请尝试提供帮助