swagger 数据处理 将传值的description和example 转化为 title:description description:example

// 递归函数,用于遍历和修改 JSON 结构
function addTitleToStrings(obj) {
for (var key in obj) {
console.log("key",key)
if (typeof obj[key] === "object") {
// 如果属性值是对象,递归处理
addTitleToStrings(obj[key]);
} else if (key === "type" && obj[key] === "string") {
// 如果属性名是 "type" 并且属性值是 "string",则添加 "title" 字段
obj["title"] = obj["description"];
obj["description"]=obj["example"];
delete obj.example;
}
}
}

// 原始 JSON 数据
var jsonData = {
"type": "object",
"title": "title",
"properties": {
"field_1": {
"type": "string",
"example": "11111",
"description": "22222"
},
"field_2": {
"type": "object",
"properties": {
"field_3": {
"type": "string",
"example": "33333",
"description": "44444"
}
},
"required": [
"field_3"
]
}
},
"required": [
"field_1",
"field_2"
]
}

// 调用递归函数来处理 JSON 数据
addTitleToStrings(jsonData);

// 输出结果
console.log(JSON.stringify(jsonData, null, 2));
VM758:4 key type
VM758:4 key title
VM758:4 key properties
VM758:4 key field_1
VM758:4 key type
VM758:4 key description
VM758:4 key field_2
VM758:4 key type
VM758:4 key properties
VM758:4 key field_3
VM758:4 key type
VM758:4 key description
VM758:4 key required
VM758:4 key 0
VM758:4 key required
VM758:4 key 0
VM758:4 key 1

 


VM758:51 {
"type": "object",
"title": "title",
"properties": {
"field_1": {
"type": "string",
"description": "11111",
"title": "22222"
},
"field_2": {
"type": "object",
"properties": {
"field_3": {
"type": "string",
"description": "33333",
"title": "44444"
}
},
"required": [
"field_3"
]
}
},
"required": [
"field_1",
"field_2"
]
}

posted @ 2023-09-12 20:11  镜湖者  阅读(52)  评论(0)    收藏  举报