Issue
I am building an Angular 7 app. In this app I let the users edit HTML that I then want to convert into JSON to store it in a way that make sense.
In short, I want to take any HTML code and process it into a JSON object. How can I do this?
Solution
I'd parse the HTML into a DOM (you can do that client-side or server-side) and then serialize the aspects of the DOM that I cared about to an object, which I'd then use JSON.stringify
on (if you really want JSON).
For instance:
function converter(dom) {
if (dom.nodeType === Node.TEXT_NODE) {
return dom.nodeValue;
}
if (dom.nodeType === Node.DOCUMENT_NODE) {
dom = dom.documentElement;
}
const obj = {};
obj.nodeType = dom.nodeType;
if (dom.nodeType === Node.ELEMENT_NODE) {
obj.tagName = dom.tagName;
obj.attributes = []; // Array.from(obj.attributes) gives us a lot of things we don't want
for (let i = 0, len = dom.attributes.length; i < len; ++i) {
const attr = dom.attributes[i];
obj.attributes.push({name: attr.name, value: attr.value});
}
obj.children = [];
for (let child = dom.firstChild; child; child = child.nextSibling) {
obj.children.push(converter(child));
}
} else {
obj.nodeValue = dom.nodeValue;
}
return obj;
}
const json = JSON.stringify(converter(document.getElementById("example")), null, 4);
console.log(json);
.as-console-wrapper {
max-height: 100% !important;
}
<div id="example" class="ex">
<span>Span 1</span>
<span>Span 2</span>
<!-- comment -->
<span>
Span 3
<span>Inner span</span>
</span>
</div>
Obviously that's just a rough sketch, not a completely baked solution.
Answered By - T.J. Crowder
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.