K
K
KambulovDanil2020-09-16 10:32:44
JavaScript
KambulovDanil, 2020-09-16 10:32:44

How to write html code to json?

How to implement line-by-line writing of html code in JSON? On JS

For example, I have a full-fledged page code, I need to write it all line by line In JSON. How can i do this? Help with advice or example

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Andrey Vasilev, 2020-09-16
@Nolis

https://toolslick.com/conversion/data/html-to-json

S
Stalker_RED, 2020-09-16
@Stalker_RED

You need to escape some characters, depending on where exactly you plan to use this JSON, the set of these characters may differ.
const foo1 = {"key":"<h1>Hello</h1>"} // valid

const foo2 = {"key":"<h1 id="zzz">Hello</h1>"} // invalid

const foo3 = {"key":"<h1 id=\"zzz\">Hello</h1>"} // valid

H
hzzzzl, 2020-09-16
@hzzzzl

function textNodesUnder(el){
  var n, a=[], walk=document.createTreeWalker(el,NodeFilter.SHOW_ELEMENT,null,false);
  while(n=walk.nextNode()) a.push(n);
  return a;
}

textNodesUnder(document.body)

^ all elements on the page in an array
/* from here */
then you can go through the array and convert it to something like tagName: textContent

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question