Ulf Dittmer wrote:There is no general conversion from HTML to JSON, nor do I think it would make much sense to define one (which isn't to say it wouldn't make sense for your purposes). If I wanted to work with HTML I'd convert it to XML (using a library like NekoHTML or TagSoup) and then use XML/DOM APIs to manipulate it.
Jesper Young wrote:Which question are you asking:
- "is there a way to convert HTML to JSON"
- "is there a way to convert JSON to HTML"
Because in the title you're asking one thing, but in the topic itself you're asking the opposite.
Bob Cen wrote:Now, do you know of an actual HTML to JSON converter? I am looking for a straight-forward Yes or No answer, not ...
Jesper Young wrote:
Bob Cen wrote:Now, do you know of an actual HTML to JSON converter? I am looking for a straight-forward Yes or No answer, not ...
In that case... no.
If you're not interested why not, then stop reading now. Otherwise...
HTML is a totally different kind of format than JSON. JSON (JavaScript Object Notation) is a way to serialize objects (you have an object, which contains key-value pairs etc.). HTML describes a page layout. That doesn't fit very well with what JSON represents. There is no clear, one-to-one way to map the structure of an HTML document to objects with key-value pairs. They are just two totally different formats, made for different purposes.
What's the reason why you need this? If you explain what your actual goal is, we might be able to help you further.
There is no clear, one-to-one way to map the structure of an HTML document to objects with key-value pairs.
Bob Cen wrote:The format of HTML and JSON (and the purpose of HTML and JSON) is irrelevant. Converters are written all the time, given the definition of what has to be converted. Granted, some conversions are not 100%, but even 99% is better than none. As far as my question and your response, which implies there is no way to convert between the two formats, is not exactly accurate. Firefox converts HTML to JSON and JSON to HTML. It does this for bookmarks.
Bob Cen wrote:Actually, this discussion has made me think about it (as I wasn't thinking of XML in my initial post). There are XML <-> JSON converters. So I could do the conversion in 2 steps HTML -> XML and then XML -> JSON (and, of couse, reverse the procedure to go the other way). Thank you. javascript:emoticon('
');
Jesper Young wrote:
I did not say there is absolutely no way to do this. But there is no clear way to do this in general. Your example with the Firefox bookmarks is a very specific example, with a specific mapping. It's not a general mapping that converts any HTML page to JSON.
If the page is in XHTML (a variant of HTML that conforms to XML syntax - i.e. all elements have open and close tags, all attributes have quoted values etc.) then you don't even need to do the HTML to XML conversion first. But you'd get a JSON document that has the same structure as the HTML, for example:
<html>
<head>
<title>Hello World</title>
</head>
<body>
<p>Some text</p>
</body>
</html>
converts to:
"html": { "head": { "title": "Hello World" }, "body": { "p": "Some text" } }
You could do that if you want to. I'm curious however why you'd want to do this, to me it looks like a strange way to use JSON.
Don't get me started about those stupid light bulbs. |