Skip to content

Functions

parseJSON()

parseJSON<T>(text, options): T

解析 JSON 文本

Type Parameters

Type Parameter
T

Parameters

ParameterTypeDescription
textstringJSON 文本
optionsParseJSONOptions<T>配置项

Returns

T

Example

ts
// 错误或者值为空时设置 {}
parseJSON('{ a: 1 }', { onNil: {} }) // 简写默认值
parseJSON('{ a: 1 }', { onNil: () => ({})  }) // 函数返回默认值
// => {}

// 错误时设为 {}
parseJSON('{ a: 1 }', { onNil: (error) => error ? {} : null  })
// => {}

// 值为空时设为 {}
parseJSON('null', { onNil: (error) => error ? null : {} })
// => {}

Interfaces

ParseJSONOptions<T>

Type Parameters

Type Parameter
T

Properties

PropertyTypeDescription
onNil?MaybeFn<T, [Error, null, string]>解析结果为 nullundefined 时执行并返回最终结果 Param 解析错误时的错误信息 Param 解析结果,固定为 nullundefined Param 解析的 JSON 文本
reviver?(this: any, key: string, value: any) => anyJSON.parse(text, reviver)