feat: compatible with json strings with curly braces {

This commit is contained in:
Gabe
2025-08-23 00:10:42 +08:00
parent c6f4fe2b7b
commit 36c1e40d64
2 changed files with 34 additions and 10 deletions

View File

@@ -267,3 +267,25 @@ export const getHtmlText = (htmlStr, skipTag = "") => {
return doc.body.innerText.trim();
};
/**
* 解析JSON字符串对象
* @param {*} str
* @returns
*/
export const parseJsonObj = (str) => {
if (!str || type(str) !== "string") {
return {};
}
try {
if (str.trim()[0] !== "{") {
str = `{${str}}`;
}
return JSON.parse(str);
} catch (err) {
//
}
return {};
};