Files
template-10582/src/utils/jsonUtils.ts
赵忠林 43106acc27 feat(pages): 添加多个页面配置和功能模块
- 新增 .editorconfig、.eslintrc、.gitignore 配置文件
- 添加管理员文章管理页面配置和功能实现
- 添加经销商申请注册页面配置和功能实现
- 添加经销商银行卡管理页面配置和功能实现
- 添加经销商客户管理页面配置和功能实现
- 添加用户地址管理页面配置和功能实现
- 添加用户聊天消息页面配置和功能实现
- 添加用户礼品管理页面配置和功能实现
2026-01-08 13:36:06 +08:00

31 lines
640 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 判断字符串是否为有效的JSON格式
* @param str 要检测的字符串
* @returns boolean
*/
export function isValidJSON(str: string): boolean {
if (typeof str !== 'string' || str.trim() === '') {
return false;
}
try {
JSON.parse(str);
return true;
} catch (error) {
return false;
}
}
/**
* 安全解析JSON失败时返回默认值
* @param str JSON字符串
* @param defaultValue 默认值
* @returns 解析结果或默认值
*/
export function safeJSONParse<T>(str: string, defaultValue: T): T {
try {
return JSON.parse(str);
} catch (error) {
return defaultValue;
}
}