jquery.serializeJSON
jquery.serializeJSON copied to clipboard
Doesn't work with ASP.NET MVC naming conventions
I tried to use this library to serialize a complex form into json to send it as model in my controller but the list items are not serialized correctly.
Name exemple : foo.bar[0].prop
Serialization :
"foo.bar" : { "0.prop" : "value1", "1.prop" : "value2", "2.prop" : "value3", }
Should be :
"foo" : { "bar" : [ {"prop" : "value1"}, {"prop": "value2"}, {"prop": "value3"} ] }
It seems you expect the plugin to handle a different syntax. This plugin works with the syntax key[nested][nested]
.
Instead of key.nested.nested:
name="foo.bar.[0].prop"
This plugin uses key[nested][nested]:
name="foo[bar][0][prop]"
In addition, you need to tell the plugin to interpret the numbers as array indexes with the option useIntKeysAsArrayIndex
:
$(myform).serializeJSON({useIntKeysAsArrayIndex: true});
This will produce the expected serialization.
Is is possible to serialize the with the dot syntax key.nested.nested?
Currently not. But I am open to a pull request suggestion to incorporate this syntax with an option like {dotSyntax: true}
. It seems this could be implemented without much effort on the internal method splitInputNameIntoKeysArray. Thanks!
Hi, I see your point. However, the syntax used by ASP.NET MVC follows a standard used by all C# developers. I think it would be useful to include this support. For your information, I've found another library that supports this syntax. Nevertheless, I'd like to thank you for your work, which has undoubtedly helped a lot of people.
For your information, I've found another library that supports this syntax.
Can you please share which library is it? Couse I have the same issue.
For your information, I've found another library that supports this syntax.
Can you please share which library is it? Couse I have the same issue.
Here is the repo : https://github.com/raphaelm22/jquery.serializeToJSON
https://github.com/raphaelm22/jquery.serializeToJSON
Thx.