Skip to content

useJsonParse

JSON.parse 的默认值版本, parse 失败时返回默认值

示例

ts
import { useJsonParse } from '@smartos-lib/utils';

// 正常使用
useJsonParse('{"a":1}'); // { a: 1 }

// 提供默认值, parse 出错时返回默认值
useJsonParse('xxx', { a: 2 }); // { a: 2 }

类型

ts
/**
 * @param text 格式化文本
 * @param defaultValue 格式化出错时的默认值
 */
function useJsonParse<T>(text?: string | null, defaultValue?: T): T;