ai/src/utils/json.ts
2025-06-21 14:01:33 +08:00

13 lines
293 B
TypeScript

/**
* 尝试从字符串中提取JSON对象
*/
export const getJsonFromString = (str: string) => {
try {
const jsonMatch = str.match(/```json\s*([\s\S]*?)\s*```/);
if (jsonMatch && jsonMatch[1]) {
return JSON.parse(jsonMatch[1]);
}
} catch (error) {}
return null;
};