My C# web method uses JavaScriptSerializer to return a JSON object in string format called jSONstring. My javascript then uses jquery.deserialize and does the following:
$("#form").deserialize(JSON.parse(jSONstring));
This seems to work fine except when mapping to an array of elements.
My HTML is as follows:
<input name="AccountID" value="" />
<input name="Account[0].name" value="" />
<input name="Account[1].name" value="" />
<input name="Account[2].name" value="" />
The JSON object returned from the server looks like:
'{"AccountID": 123, "Account":[{"name": "AccountName1"},{"name": "AccountName2"},{"name": "AccountName3"}]}'
After calling deserialize the AccountID is updated but the account names are never updated to AccountName1, AccountName2, AccountName3.
I tried renaming my input elements to the following and it still doesn’t work:
<input name="Account.name" value="" />
<input name="Account.name" value="" />
<input name="Account.name" value="" />
What am I doing wrong?
That format is not supported by the plugin, it only supports data generated from jQuery.serialize, jQuery.serializeArray and jQuery.serializeObject (unofficial). That said, it would not be hard to write a small function that would convert the returned JSON object you have into one of the formats listed above. Another option would be using a different method of serialization, such as parametizing it.