How to use this?
json = to_exploded_object( $('form_test').serialize() );
or
json = to_exploded_object( Object.toJSON( $
('form_test').serialize() ) );
Look the result in both examples, this don't work like i expected, but
i get the ideia to don't use eval. thx both.
{ 0:""", 1:"c", 2:"o", 3:"n", 4:"c", 5:"e", 6:"s",
7:"s", 8:"i", 9:"o", 10:"n", 11:"a", 12:"r", 13:"i",
14:"a", 15:".", 16:"c", 17:"a", 18:"r", 19:"r",
20:"o", 21:".", 22:"m", 23:"a", 24:"r", 25:"c",
26:"a", 27:".", 28:"c", 29:"o", 30:"d", 31:"i", 32:"g",
33:"o", 34:"=", 35:"2", 36:"0", 37:"&",
38:"c", 39:"o", 40:"n", 41:"c", 42:"e", 43:"s",
44:"s", 45:"i", ....
On 11 jun, 22:48, "Frederick Polgardy" <[EMAIL PROTECTED]> wrote:
> Right, all the uses of eval were unnecessary.
>
> I came up with a quick utility along these lines, that allows you to pass in
> an object like:
>
> {"a.b.c": 1, "a.b.d":2, "a.b.e[0].f": 3, "a.b.e[1].g": 4}
>
> Which you might get from a Prototype form utility function, and get back an
> exploded object like:
>
> {"a": {"b": {"c":1, "d":2, "e": [{"f": 3}, {"g": 4}]}}}
>
> Which is suitable for passing to Object.toJSON(). It basically just handles
> intermediate objects that can be ordinary values or arrays, but that handles
> all the cases I can think of. Let me know what you think.
>
> function to_exploded_object(object) {
> var root = {};
>
> $H(object).each(function(property) {
> var current = root,
> path = property.key.split("."),
> last = path.pop();
>
> function set_and_advance_key(key, value) {
> var match = key.match(/^(\w+)(?:\[(\d+)\])?/),
> name = match[1],
> index = match[2];
>
> if (index) {
> index = parseInt(index);
> current[name] = current[name] || [];
> current[name][index] = current[name][index] || value;
> current = current[name][index];
> } else {
> current[name] = current[name] || value;
> current = current[name];
> }
> }
>
> path.each(function(key) { set_and_advance_key(key, {}); });
> set_and_advance_key(last, property.value);
> });
>
> return root;
> }
>
> -Fred
>
> On Wed, Jun 11, 2008 at 4:43 PM, kangax <[EMAIL PROTECTED]> wrote:
>
> > Fair enough, but still makes little sense to use eval:
>
> > function toObject(str) {
> > var result = { };
> > str.split('.').inject(result, function(parent, child) {
> > return parent[child] = parent[child] || { };
> > });
> > return result;
> > };
>
> > toObject('foo.bar.baz'); // { foo: { bar: baz: { }}}
>
> --
> Science answers questions; philosophy questions answers.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Spinoffs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---