gpt4 book ai didi

javascript - 根据我的项目中的以下要求转换json

转载 作者:行者123 更新时间:2023-12-02 23:44:20 25 4
gpt4 key购买 nike

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,如上所述。

我必须在同一个转换类中执行此操作,需要帮​​助。提前致谢。

场景:休息服务 >>> JSON

{
error_description: "Already Rejected",
error_status: "Faliure",
error_code: "401"
}

transforming json(because I cant show the field name as error_description,It should be Description) and showing as form in ui as editable fields again editing data and sending the json to rest service

{
"Description": "Already Rejected",
"Status": "Faliure",
"Error Code": "401"
}

但是现在 json 已经被转换,所以休息服务不会采取。所以我再次需要 json 作为

{
error_description: "Already Rejected",
error_status: "Faliure",
error_code: "401"
}

我无法在 Rest 服务中添加 json 转换属性,因为它来自第 3 方。

最佳答案

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);

我更新了,请尝试一下可能会有帮助

关于javascript - 根据我的项目中的以下要求转换json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55917874/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com