Monday, October 12, 2009

How to write a JSON string from .NET c# code for AJAX usage


Assume the variable name of the List is _List

System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(_List.GetType());
System.IO.MemoryStream ms = new System.IO.MemoryStream();
serializer.WriteObject(ms, _List);
string json = Encoding.UTF8.GetString(ms.ToArray());