Its very easy to serialize an object to .NET
Simply create some object, normally a custom class with some attributes.
Normally you have a list of these and you want to serialize to JSON to use it from client side code.
If you do the following
you will end up with a JSON array that you were looking for:
However if you try to deserialize this back into an object by running
var json = new System.Web.Script.Serialization.JavaScriptSerializer(); var result = json.Deserialize<ShoppingCartItem[]>(jsonItemArray);
You will get a NULL type Exception.
The solution is to create a simple resolver as follows
2) Use it to serialize
var s = new System.Web.Script.Serialization.JavaScriptSerializer(new XYZ.Util.ManualResolver()); string resultJs = s.Serialize(result); lblJs.Text = string.Format("<script>var resultObj = {0};</script>", resultJs);
You will get something like the following this time (note new __type included this time)
3) Use it to deserialize
References: Comment by Manuel Abadia on ASP.NET AJAX Extensions Internals – Web Service Proxy Generation