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

View File

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