38 lines
1.0 KiB
JavaScript
38 lines
1.0 KiB
JavaScript
export function isEmptyJsonObject(jsonString) {
|
|
try {
|
|
const obj = JSON.parse(jsonString);
|
|
return isEmptyObject(obj);
|
|
}
|
|
catch (error) {
|
|
// 如果解析失败,返回 false
|
|
return false;
|
|
}
|
|
}
|
|
export function isEmptyObject(obj) {
|
|
// 处理 null 或 undefined
|
|
if (obj === null || obj === undefined) {
|
|
return true;
|
|
}
|
|
// 处理空字符串
|
|
if (typeof obj === 'string' && obj === '') {
|
|
return true;
|
|
}
|
|
// 如果是数组
|
|
if (Array.isArray(obj)) {
|
|
// 所有元素都为空才返回 true
|
|
return obj.length === 0 || obj.every(item => isEmptyObject(item));
|
|
}
|
|
// 如果是对象
|
|
if (typeof obj === 'object') {
|
|
const keys = Object.keys(obj);
|
|
// 空对象返回 true
|
|
if (keys.length === 0) {
|
|
return true;
|
|
}
|
|
// 递归检查所有值
|
|
return keys.every(key => isEmptyObject(obj[key]));
|
|
}
|
|
// 其他类型(数字、布尔值等)视为非空
|
|
return false;
|
|
}
|