(obj)
| 1660 | |
| 1661 | // 新增:递归解包嵌套JSON字符串的函数 |
| 1662 | function deepParseJSONStrings(obj) { |
| 1663 | if (Array.isArray(obj)) { |
| 1664 | return obj.map(item => { |
| 1665 | // 对于数组中的字符串元素,也尝试解析为JSON |
| 1666 | if (typeof item === 'string' && item.trim()) { |
| 1667 | try { |
| 1668 | const parsed = JSON.parse(item); |
| 1669 | // 只递归对象或数组,且排除BigInt结构(如{s,e,c})和纯数字 |
| 1670 | if ( |
| 1671 | typeof parsed === 'object' && |
| 1672 | parsed !== null && |
| 1673 | (Array.isArray(parsed) || Object.prototype.toString.call(parsed) === '[object Object]') && |
| 1674 | !( |
| 1675 | parsed && |
| 1676 | typeof parsed.s === 'number' && |
| 1677 | typeof parsed.e === 'number' && |
| 1678 | Array.isArray(parsed.c) && |
| 1679 | Object.keys(parsed).length === 3 |
| 1680 | ) |
| 1681 | ) { |
| 1682 | return deepParseJSONStrings(parsed); |
| 1683 | } |
| 1684 | } catch (e) { |
| 1685 | // 解析失败,保持原字符串 |
| 1686 | } |
| 1687 | } |
| 1688 | return deepParseJSONStrings(item); |
| 1689 | }); |
| 1690 | } else if (typeof obj === 'object' && obj !== null) { |
| 1691 | const newObj = {}; |
| 1692 | for (const key in obj) { |
| 1693 | if (!obj.hasOwnProperty(key)) continue; |
| 1694 | const val = obj[key]; |
| 1695 | if (typeof val === 'string' && val.trim()) { |
| 1696 | try { |
| 1697 | const parsed = JSON.parse(val); |
| 1698 | // 只递归对象或数组,且排除BigInt结构(如{s,e,c})和纯数字 |
| 1699 | if ( |
| 1700 | typeof parsed === 'object' && |
| 1701 | parsed !== null && |
| 1702 | (Array.isArray(parsed) || Object.prototype.toString.call(parsed) === '[object Object]') && |
| 1703 | !( |
| 1704 | parsed && |
| 1705 | typeof parsed.s === 'number' && |
| 1706 | typeof parsed.e === 'number' && |
| 1707 | Array.isArray(parsed.c) && |
| 1708 | Object.keys(parsed).length === 3 |
| 1709 | ) |
| 1710 | ) { |
| 1711 | newObj[key] = deepParseJSONStrings(parsed); |
| 1712 | continue; |
| 1713 | } |
| 1714 | } catch (e) { |
| 1715 | // 解析失败,保持原值 |
| 1716 | } |
| 1717 | } |
| 1718 | newObj[key] = deepParseJSONStrings(val); |
| 1719 | } |
no test coverage detected