pre {outline: 1px solid #ccc; padding: 5px; margin: 5px; }
.string { color: green; }
.number { color: darkorange; }
.boolean { color: blue; }
.null { color: magenta; }
.key { color: red; }
JSON格式化$(document).ready(function(){
var result = {
"status": 1,
"data": {
"total": 3,
"page": 1,
"limit": 10,
"list": [
{
"title": '测试1',
"title1": '测试2',
"title3": '中文',
"title4": '简体'
}
]
},
"message": "success",
"$dateTime": "2019-05-17 06:35:24",
"$timestamp": 1558074924
};
$('#result').html(syntaxHighlight(result));
});
function syntaxHighlight(json) {
if (typeof json != 'string') {
json = JSON.stringify(json, undefined, 2);
}
json = json.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 '' + match + '';
});
}