JSON格式化

该网页是一个HTML文档,实现了一个简单的在线JSON格式化功能。用户可以输入JSON数据,点击按钮进行格式化,并能直接复制格式化后的文本。页面还包含错误检测,若输入不符合JSON规范,会显示错误提示。
摘要由CSDN通过智能技术生成
<!DOCTYPE html>
<html lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<meta http-equiv="Expires" content="0">
	<meta http-equiv="Pragma" content="no-cache">
	<meta http-equiv="Cache-Control" content="no-cache">
	<meta http-equiv="Cache" content="no-cache">
	<title>JSON格式化</title>
	<script type="text/javascript">
	let source;
	let formatText;
	let successInfo;
	let jsonFormatErr;
	window.onload = function() {
		source = document.getElementById("source");
		formatText = document.getElementById("formatText");
		successInfo = document.getElementById("successInfo");
		jsonFormatErr = document.getElementById("jsonFormatErr");
	}
	function addTabs(size) {
		let result = "";
		for (let y = 0; y < size; y++) {
			result += "\t";
		}
		return result;
	}
	
	function copyTxt() {
		if(!navigator || !navigator.clipboard) {
			formatText.focus();
			formatText.select();
			document.execCommand("copy");
		} else {
			navigator.clipboard.writeText(formatText.value).then(
				function() {
					successInfo.style.visibility = "";
					// 2秒后隐藏
					setTimeout(function() {
						successInfo.style.visibility = "hidden";
					}, 1000);
				},
				function(err) {
				}
			);
		}
	}
	
	function includes(arr, node) {
		for (let i = 0, len = arr.length; i < len; i++) {
			if (arr[i] === node) {
				return true;
			}
		}
		return false;
	}
	
	function formatJson(sourceText) {
		let now;
		let add;
		let tier = 0;
		let inStr = false;
		let tran = false;
		let tranTxt = "";
		let lastTxt = "";
		let result = "";
		for (let i = 0, len = sourceText.length; i < len; i++) {
			now = sourceText[i];
			if (inStr) {
				add = now;
				switch(now) {
					case "\\":
						if (!tran) {
							tran = true;
						}
						continue;
					case "'":
						if (tranTxt === now && !tran) {
							inStr = false;
						}
						break;
					case "\"":
						if (tranTxt === now && !tran) {
							inStr = false;
						}
						break;
				}
				tran = false;
			} else {
				if ("‘" === now) {
					now = "'";
				} else if ("“" === now) {
					now = "\"";
				} else if ("【" === now) {
					now = "[";
				} else if ("】" === now) {
					now = "]";
				} else if ("{" === now) {
					now = "{";
				} else if ("}" === now) {
					now = "}";
				} else if ("," === now) {
					now = ",";
				} else if (":" === now) {
					now = ":";
				}
				add = now;
				switch(now) {
					case "'":
					case "\"":
						tranTxt = now;
						inStr = true;
						break;
					case "{":
					case "[":
						tier++;
						break;
					case "}":
					case "]":
						tier--;
						break;
					case "\\":
					case "\t":
					case "\r":
					case "\n":
					case " ":
						continue;
				}
				if (includes([',','{','['], lastTxt)) {
					if (includes(['{','['], now)) {
						add = "\n" + addTabs(tier - 1) + add;
					} else {
						add = "\n" + addTabs(tier) + add;
					}
				} else {
					if (includes(['}',']'], now)) {
						add = "\n" + addTabs(tier) + add;
					}
				}
			}
			lastTxt = now;
			result += add;
		}
		if (tier == 0 && !tran) {
			jsonFormatErr.style.visibility = "hidden";
		} else {
			jsonFormatErr.style.visibility = "";
		}
		return result;
	}
	
	function format() {
		formatText.value = formatJson(source.value);
	}
	</script>
</head>
<body>
	<div>
		<textarea id = "source" style = "width: 800px; height: 300px;"></textarea>
	</div>
	<div>
		<div style = "display: inline;">
			<button onclick = "format()" style = "width: 80px; height: 30px;">格式化</button>
			<button onclick = "copyTxt()" style = "width: 80px; height: 30px;">复制文本</button>
			<text id="successInfo" style = "visibility: hidden; color:green; font-weight: bold;">复制成功</text>
			<text id="jsonFormatErr" style = "visibility: hidden; color:red; font-weight: bold;">不符合JSON规范</text>
		</div>
		<div>
		<textarea id = "formatText" style = "width: 800px; height: 300px;" readonly="readonly">
		格式后的文本,不可编辑
		可复制重复格式化
		</textarea>
		</div>
	</div>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值