Skip to content

Instantly share code, notes, and snippets.

@jtacoma
Created February 5, 2011 00:36
Show Gist options
  • Save jtacoma/812064 to your computer and use it in GitHub Desktop.
Save jtacoma/812064 to your computer and use it in GitHub Desktop.
Converting HTML to JSON with jQuery. Just a gist...
<dl>
<dt>selector</dt>
<dd>dl</dd>
<dt>apply</dt>
<dd>
<ul>
<li>
<dl>
<dt>selector</dt>
<dd>&gt; dt</dd>
<dt>key</dt>
<dd></dd>
<dt>value</dt>
<dd>~ dd</dd>
</dl>
</li>
</ul>
</dd>
</dl>
function jquery2json(nodes) {
var result = {};
nodes.find("dl").find("> dt").each(function (match) {
var key = match.text();
var value = match.find("~ dd");
result[match.text()] = jquery2json(value) || value.text();
});
return result;
}
[
{
selector: "dl",
apply:
[
{
selector: "> dt",
key: "",
value: "~ dd",
}
]
},
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment