Created
February 5, 2011 00:36
-
-
Save jtacoma/812064 to your computer and use it in GitHub Desktop.
Converting HTML to JSON with jQuery. Just a gist...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dl> | |
<dt>selector</dt> | |
<dd>dl</dd> | |
<dt>apply</dt> | |
<dd> | |
<ul> | |
<li> | |
<dl> | |
<dt>selector</dt> | |
<dd>> dt</dd> | |
<dt>key</dt> | |
<dd></dd> | |
<dt>value</dt> | |
<dd>~ dd</dd> | |
</dl> | |
</li> | |
</ul> | |
</dd> | |
</dl> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
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