Created
November 28, 2009 19:48
-
-
Save eltiare/244625 to your computer and use it in GitHub Desktop.
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
<html> | |
<head> | |
<title>Testing</title> | |
</head> | |
<body> | |
<form action="./" method="post"> | |
<input type="text" name="question[id]"/> | |
<input type="text" name="question[text]" /> | |
<input type="text" name="question[user][email]" /> | |
</form> | |
<script type="text/javascript"> | |
inputs = document.getElementsByTagName('input'); | |
json_var = {}; | |
for (i=0; input = inputs[i]; i++) { | |
parts = input.name.split(/[\[\]]+/); | |
if (parts.length > 1) { | |
parts.pop(); // The last value will always be an empty string | |
if (json_var[parts[0]] === undefined) { json_var[parts[0]] = {}; } | |
obj = json_var[parts[0]]; | |
for (i2=1; name = parts[i2]; i2++) { | |
// If last iteration | |
if (i2 == parts.length - 1) { | |
obj[name] = input.value; | |
} else { | |
if (obj[name] === undefined) { obj[name] = {}; } | |
obj = obj[name]; | |
} | |
} | |
} else { | |
json_var[parts[0]] = input.value; | |
} | |
} | |
console.info(json_var); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment