您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“html如何轉(zhuǎn)json”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
1.json概念
json(JavaScript Object Notation, JS 對(duì)象標(biāo)記) 是一種輕量級(jí)的數(shù)據(jù)交換格式。它起源于JavaScript的數(shù)據(jù)對(duì)象,之后獨(dú)立成為一種web較為流行的數(shù)據(jù)交換標(biāo)準(zhǔn)。
json的語(yǔ)法,是非常明顯的鍵值對(duì)結(jié)構(gòu),比較利于理解:
{ "note":{ "to":"George", "from":"John", "heading":"Reminder", "body":"Don't forget the meeting!" } }
2.實(shí)例
import { Parser } from "htmlparser2" const numberValueRegexp = /^\d+$/ const zeroValueRegexp = /^0[^0\s].*$/ const scriptRegexp = /^script$/i const styleRegexp = /^style$/i const selfCloseTagRegexp = /^(meta|base|br|img|input|col|frame|link|area|param|embed|keygen|source)$/i const TAG = 'tag' const TEXT = 'text' const COMMENT = 'comment' /** * 去除前后空格 */ export const trim = val => { return (val || '').replace(/^\s+/, '').replace(/\s+$/, '') } /** * 首字母大寫(xiě) */ export const capitalize = word => { return (word || '').replace(/( |^)[a-z]/, c => c.toUpperCase()) } /** * 駝峰命名法/小駝峰命名法, 首字母小寫(xiě) */ export const camelCase = key => { return (key || '').split(/[_-]/).map((item, i) => i === 0 ? item : capitalize(item)).join('') } /** * 大駝峰命名法,首字母大寫(xiě) */ export const pascalCase = key => { return (key || '').split(/[_-]/).map(capitalize).join('') } export const isPlainObject = obj => { return Object.prototype.toString.call(obj) === '[object Object]' } /** * 行內(nèi)樣式轉(zhuǎn)Object */ export const style2Object = (style) => { if (!style || typeof style !== 'string') { return {} } const styleObject = {} const styles = style.split(/;/) styles.forEach(item => { const [prop, value] = item.split(/:/) if (prop && value && trim(value)) { const val = trim(value) styleObject[camelCase(trim(prop))] = zeroValueRegexp.test(val) ? 0 : numberValueRegexp.test(val) ? Number(val) : val } }) return styleObject } export const toJSON = (html, options) => { options = Object.assign({ skipStyle: false, skipScript: false, pureClass: false, pureComment: false }, options) const json = [] let levelNodes = [] const parser = new Parser({ onopentag: (name, { style, class: classNames, ...attrs } = {}) => { let node = {} if ((scriptRegexp.test(name) && options.skipScript === true) || (styleRegexp.test(name) && options.skipStyle === true)) { node = false } else { if (options.pureClass === true) { classNames = '' } node = { type: TAG, tagName: name, style: style2Object(style), inlineStyle: style || '', attrs: { ...attrs }, classNames: classNames || '', classList: options.pureClass ? [] : (classNames || '').split(/\s+/).map(trim).filter(Boolean), children: [] } } if (levelNodes[0]) { if (node !== false) { const parent = levelNodes[0] parent.children.push(node) } levelNodes.unshift(node) } else { if (node !== false) { json.push(node) } levelNodes.push(node) } }, ontext(text) { const parent = levelNodes[0] if (parent === false) { return } const node = { type: TEXT, content: text } if (!parent) { json.push(node) } else { if (!parent.children) { parent.children = [] } parent.children.push(node) } }, oncomment(comments) { if (options.pureComment) { return } const parent = levelNodes[0] if (parent === false) { return } const node = { type: COMMENT, content: comments } if (!parent) { json.push(node) } else { if (!parent.children) { parent.children = [] } parent.children.push(node) } }, onclosetag() { levelNodes.shift() }, onend() { levelNodes = null } }) parser.done(html) return json } const setAttrs = (attrs, results) => { Object.keys(attrs || {}).forEach(k => { if (!attrs[k]) { results.push(k) } else { results.push(' ', k, '=', '"', attrs[k], '"') } }) } const toElement = (elementInfo, results) => { switch (elementInfo.type) { case TAG: const tagName = elementInfo.tagName results.push('<', tagName) if (elementInfo.inlineStyle) { results.push(' style="', elementInfo.inlineStyle, '"') } if (elementInfo.classNames) { results.push(' class="', elementInfo.classNames, '"') } setAttrs(elementInfo.attrs, results) if (selfCloseTagRegexp.test(tagName)) { results.push(' />') } else { results.push('>') if (Array.isArray(elementInfo.children)) { elementInfo.children.forEach(item => toElement(item, results)) } results.push('</', tagName, '>') } break; case TEXT: results.push(elementInfo.content) break; case COMMENT: results.push("<!-- ", elementInfo.content, " -->") break; default: // ignore } } export const toHTML = json => { json = json || [] if (isPlainObject(json)) { json = [json] } const results = [] json.forEach(item => toElement(item, results)) return results.join('') }
“html如何轉(zhuǎn)json”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。