/** * 将JSON格式的HTML结构转换为HTML字符串 * @param {Object} json - JSON格式的HTML结构 * @returns {string} HTML字符串 */ function jsonToHtml(json) { if (!json || typeof json !== 'object') { return ''; } function parseElement(obj) { if (obj === null || obj === undefined) { return ''; } if (typeof obj === 'string') { return obj; } if (Array.isArray(obj)) { return obj.map(item => parseElement(item)).join(''); } // 检查是否为文本节点 (#text) if ('#text' in obj) { let tagName = 'div'; // 默认标签 let attributes = ''; let content = obj['#text'] || ''; // 处理属性和其他设置 for (const key in obj) { if (key === '#text') continue; if (key.startsWith('@')) { // 属性 (@class, @id等) const attrName = key.substring(1); attributes += ` ${attrName}="${obj[key]}"`; } else if (['div', 'span', 'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'section', 'header', 'footer', 'main', 'article', 'aside'].includes(key)) { // 子元素标签 tagName = key; content = parseElement(obj[key]); } } return `<${tagName}${attributes}>${content}${tagName}>`; } // 普通元素节点 let tagName = 'div'; // 默认标签 let attributes = ''; let content = ''; for (const key in obj) { if (key.startsWith('@')) { // 属性 (@class, @id等) const attrName = key.substring(1); attributes += ` ${attrName}="${obj[key]}"`; } else if (['div', 'span', 'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'section', 'header', 'footer', 'main', 'article', 'aside', 'ul', 'ol', 'li', 'table', 'thead', 'tbody', 'tr', 'td', 'th'].includes(key)) { // 明确指定的标签类型 tagName = key; content = parseElement(obj[key]); } else if (key === 'style') { // style标签特殊处理 content += ``; } else if (key === 'title') { // title标签特殊处理 content += `