Geeks With Blogs
AzamSharp Some day I will know everything. I hope that day never comes.

ASP.NET AJAX framework includes the System.Web.Script.Serialization which helps to serialize the .NET object to a JSON representation string. I am using ASP.NET 2.0 callbacks to call the server side method using the client side code.

// Create JSON String

public void CreateJSONString()

{

StringBuilder sb = new StringBuilder();

Customer customer = new Customer();

customer.FirstName = "Mohammad";

customer.LastName = "Azam";

List<Customer> list = new List<Customer>();

list.Add(customer);

list.Add(new Customer("John", "Doe"));

list.Add(new Customer("Mary", "Kate")); 

System.Runtime.Serialization.DataContractJsonSerializer json =

new System.Runtime.Serialization.DataContractJsonSerializer(list.GetType());

 

 

MemoryStream ms = new MemoryStream();

XmlDictionaryWriter writer = JsonReaderWriterFactory.CreateJsonWriter(ms);

json.WriteObject(ms,list);

writer.Flush(); 

_jsonString = Encoding.Default.GetString(ms.GetBuffer());

 

}

_jsonString is a simple class level variable which is used to get the final JSON string. Finally, the JSON string is returned by the GetCallbackResult method. When using the DataContractJsonSerializer class your class should use DataContract and DataMember attributes as I have shown below:

[DataContract]

public

class Customer

{

private int _id;

private string _firstName;

private string _lastName;

 

public int Id

{

get { return _id; }

set { _id = value; }

}

[DataMember]

public string FirstName

{

get { return _firstName; }

set { _firstName = value; }

}

[DataMember]

public string LastName

{

get { return _lastName; }

set { _lastName = value; }

}

 

public Customer(string firstName, string lastName)

{

_firstName = firstName;

_lastName = lastName;

}

public Customer()

{

 

}

}

public string GetCallbackResult()

{

return _jsonString;

}

Now, on the client side we just get the data (JSON String) and creates the interface.

function

ReceiveServerData(response)

{

var customers = eval("(" + response + ")");

var ul = document.createElement("ul");

 

for(i=0; i<customers.length; i++)

{

var li = document.createElement("li");

li.innerHTML = customers[i].FirstName + " " + customers[i].LastName;

ul.appendChild(li);

}

 

// customerList is a DIV element

document.getElementById("customerList").appendChild(ul);

 

 

}

The above code will throw the JavaScript error "__pendingCallbacks async is null or not an object". To remove the error simply remove the "i" variable in the loop with some other variable name.

ASP.NET AJAX framework also includes the JavaScriptSerializer class which can be used to serialize the objects to JSON string. Check out the code below:

JavaScriptSerializer

js = new JavaScriptSerializer();

js.Serialize(list, sb);

_jsonString = sb.ToString();

Much simple but obsolete ;)

I also created the screencast which covered the second way of creating JSON string. You can view the screencast using the following link:

Using JavaScriptSerializer Class to Serialize Objects to JSON Posted on Sunday, November 4, 2007 1:12 PM | Back to top


Comments on this post: Creating JSON String Using DataContractJsonSerializer Class

# re: Creating JSON String Using DataContractJsonSerializer Class
Requesting Gravatar...
I haven't found the need to mark serializable types with DataContract, Serializable seems to be enough.

http://west-wind.com/weblog/ShowPost.aspx?id=218001
Left by Vijay Santhanam on Jan 03, 2008 3:49 AM

Your comment:
 (will show your gravatar)


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net