保存为.html 浏览器打开即可,话不多说上代码。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>测试</title>
<style>
pre {outline: 1px solid #ccc; padding: 5px; margin: 5px; }
.string { color: green; }
.number { color: darkorange; }
.boolean { color: blue; }
.null { color: magenta; }
.key { color: red; }
</style>
<script type="text/javascript">
function syntaxHighlight(json) {
if (typeof json != 'string') {
json = JSON.stringify(json, undefined, 2);
}
json = json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function(match) {
var cls = 'number';
if (/^"/.test(match)) {
if (/:$/.test(match)) {
cls = 'key';
} else {
cls = 'string';
}
} else if (/true|false/.test(match)) {
cls = 'boolean';
} else if (/null/.test(match)) {
cls = 'null';
}
return '<span class="' + cls + '">' + match + '</span>';
});
}
</script>
</head>
<body>
<div id="res">{"data":[{"partner":"baidu","platAccount":"baidu","uniId":"12345678987654567"}],"flag":"success","message":null}</div>
<pre style="white-space: pre-wrap; word-wrap: break-word;" id="result">
</pre>
<script type="text/javascript">
var songResJson=document.getElementById('res').innerText
ss=JSON.parse(songResJson)
document.getElementById('result').innerHTML = syntaxHighlight(ss);
</script>
</body>
</html>
|