This commit is contained in:
u2nyakim 2025-08-27 18:00:37 +08:00
parent f50b215653
commit e2d79ce1ef

View File

@ -145,18 +145,21 @@
*/ */
function formatJson(jsonString, indentSpaces = 2) { function formatJson(jsonString, indentSpaces = 2) {
// //
if (typeof jsonString !== 'string') {
throw new Error('输入必须是字符串');
}
// JSONJavaScript // JSONJavaScript
let parsedObj; let parsedObj;
try { if (typeof jsonString === 'string') {
parsedObj = JSON.parse(jsonString); // throw new Error('');
} catch (e) { try {
throw new Error('无效的JSON格式: ' + e.message); parsedObj = JSON.parse(jsonString);
} catch (e) {
throw new Error('无效的JSON格式: ' + e.message);
}
}else{
parsedObj = jsonString;
} }
// //
function format(obj, depth) { function format(obj, depth) {
// //