<!DOCTYPE html>
<html lang="zh_cn">
<head>
<meta charset="UTF-8">
<title>JSON转Markdown表格</title>
</head>
<style>
body {
padding: 20px;
}
div.input-group {
width: 30%;
}
div.input-group > label {
display: inline-block;
margin-bottom: 5px;
font-weight: 700;
}
div.input-group > textarea {
width: 100%;
height: 200px;
}
</style>
<body>
<div class="input-group">
<label>转换源:</label><textarea id="source"></textarea>
</div>
<p>
<button id="convert">转换</button>
<button id="convert_param">转换为入参</button>
<button id="convert_result">转换为出参</button>
</p>
<div class="input-group">
<label>转换后:</label><textarea id="target"></textarea>
</div>
<script type="text/javascript">
var source = document.querySelector("#source"),
target = document.querySelector("#target"),
convert = document.querySelector("#convert"),
convert_param = document.querySelector("#convert_param"),
convert_result = document.querySelector("#convert_result"),
json_table_data, t, instru, remark;
convert.addEventListener("click", function () {
try {
json_table_data = '| 参数名 | 类型 | 说明 |\n' +
'|:-------|:-------|:-------|\n';
t = 0;
change(JSON.parse(source.value));
} catch (e) {
alert(e);
}
});
convert_param.addEventListener("click", function () {
try {
json_table_data = '| 参数名 | 必选 | 类型 | 说明 | 备注 |\n' +
'|:-------|:-------|:-------|:-------|:-------|\n';
t = 1;
change(JSON.parse(source.value));
} catch (e) {
alert(e);
}
});
convert_result.addEventListener("click", function () {
try {
json_table_data = '| 参数名 | 类型 | 说明 | 备注 |\n' +
'|:-------|:-------|:-------|:-------|:-------|\n';
t = 2;
change(JSON.parse(source.value));
} catch (e) {
alert(e);
}
});
/**
*
* @param data json对象
*/
function change(data) {
var level_str = "- ";
if (arguments.length > 1) {
var level;
arguments[1] > 0 ? level = arguments[1] : level = 1;
for (var i = 0; i < level; i++) {
level_str += "- ";
}
}
for (var key in data) {
var value = data[key];
var type = typeof(value);
autocomplete(key);
if (type == "object") {
if (t == 1) {
json_table_data += ' | ' + level_str + key + ' | 是 | ' + type + ' | '+instru+' | '+remark+' |\n';
} else if (t == 2) {
json_table_data += ' | ' + level_str + key + ' | ' + type + ' | '+instru+' | '+remark+' |\n';
} else {
json_table_data += ' | ' + level_str + key + ' | ' + type + ' | '+ instru +' |\n';
}
if (value instanceof Array) {
var j = level + 1;
change(value[0], j);
continue;
}
change(value, level);
} else {
if (t == 1) {
json_table_data += ' | ' + key + '| 是 |' + type + ' | '+instru+' | '+remark+' |\n';
} else if (t == 2) {
json_table_data += ' | ' + key + ' |' + type + ' | '+instru+' | '+remark+' |\n';
} else {
json_table_data += ' | ' + key + ' |' + type + ' | '+ instru +' |\n';
}
}
}
target.value = json_table_data;
}
/**
* 自动填充
* @param key
*/
function autocomplete(key) {
if (key == "account")
instru = "登陆账号", remark = "任意账号";
else if (key == "accounCid")
instru = "账号cid", remark = "无";
else if (key == "accountType")
instru = "账号类型", remark = "1:医生<br>2:患者";
else if (key == "appCode")
instru = "App 编号", remark = "如:肿瘤app医生端:A000,肝病app医生端:B000";
else if (key == "jsonData")
instru = "请求方法的业务参数封装", remark = "无";
else if (key == "ts")
instru = "随机数", remark = "如:36";
else if (key == "digest")
instru = "摘要值,验证请求的有效性", remark = "account +ts+imeiuuid+appCode+ VERSION_SECRET_KEY;";
else if (key == "imeiuuid")
instru = "设备ID", remark = "android的IMEI,ios的UUID";
else if (key == "sourceType")
instru = "请求来源", remark = "取值范围:andriod,ios,pc,weixin";
else if (key == "msgCode")
instru = "消息码", remark = "成功:BSxxx<br>失败:BExxx";
else if (key == "message")
instru = "消息", remark = "无";
else if (key == "data")
instru = "数据", remark = "返回的数据";
else if (key == "resultJson")
instru = "方法执行结果对象或集合", remark = "无";
else if (key == "resultCode")
instru = "返回编码", remark = "100:成功<br> 104:参数异常<br> 108:没有权限<br> 116:系统异常<br> 132:未登录<br> 164:用户名或密码错误<br> 228: 登陆信息过期<br> 356:踢下线<br> 612:账号被锁定";
else if (key == "ext")
instru = "扩展信息", remark = "无";
else if (key == "doctorCid")
instru = "医生cid", remark = "无";
else if (key == "patientCid")
instru = "患者cid", remark = "无";
else if (key == "assistantCid")
instru = "助手cid", remark = "无";
else if (key == "pharmacyCid")
instru = "药店cid", remark = "无";
else if (key == "medicineCid")
instru = "药品cid", remark = "无";
else if (key == "cid")
instru = "业务id", remark = "无";
else if (key == "id")
instru = "自增型主键", remark = "无";
else if (key == "dataStatus")
instru = "数据状态", remark = "0:正常<br> 1:删除";
else if (key == "version")
instru = "版本号", remark = "无";
else if (key == "createDatetime")
instru = "创建时间", remark = "无";
else if (key == "updateDatetime")
instru = "更新时间", remark = "无";
else if (key == "pageSize")
instru = "页大小", remark = "无";
else if (key == "currentPage")
instru = "第几页", remark = "无";
else
instru = "无", remark = "无";
}
</script>
</body>
</html>
Json转Markdown表格代码实现
最新推荐文章于 2024-09-28 06:45:00 发布