Ahmad Masykur

Share your knowledge although one function!

About the author

Ahmad Masykur is a Software Architecture Engineer at PT. Freeport Indonesia Jakarta Indonesia.
In this blog, I share things of interest to me. Most topics are likely to be related to software development, but don't hold me to it.

Certificates



Awards


Powered by

Widget Prayer Time not found.

There is an error in XML document (4, 16278).X

Page List

Validators


Ahmad Masykur

JSON Serializer & Deserializer

Saya ingin berbagi sedikit tips mengenai JSON serializer dan deserializer yang ada di ASP.NET AJAX. Dengan menggunakan ASP.NET Ajax pertukaran data antara client (JavaScript) dengan server menjadi gampang banget. Kita tidak perlu lagi pake XMLyang rumit. Dengan JSON data mudah banget di-parse.

Contoh code di client (JS):

function getFilterData() { var dateFormat = '<%= HelpdeskSystem.DateFormat %>'; var filter = { instance: $get('<%= instanceList.ClientID %>').value, requestNo: $get('<%= requestNoTextBox.ClientID %>').value, category: $get('<%= categoryList.ClientID %>').value, itemCategory: $get('<%= itemAffectedList.ClientID %>').value, ticketGroup: $get('<%= ticketGroupList.ClientID %>').value, status: $get('<%= statusList.ClientID %>').value, requestType: $get('<%= typeList.ClientID %>').value, startDate: Date.parseLocale($get('<%= dateFromTextBox.ClientID %>').value, dateFormat), finishDate: Date.parseLocale($get('<%= dateToTextBox.ClientID %>').value, dateFormat), requestedFor: $get('requestedForEmployeeId').value, assigneeId: ($get('<%= assigneeForEmployeeId.ClientID %>') != null) ? $get('<%= assigneeForEmployeeId.ClientID %>').value : $get('<%= assigneeForEmployeeIdLabel.ClientID %>').innerHTML }; return Sys.Serialization.JavaScriptSerializer.serialize(filter); // Serialize object to JSON string }

Di server side (C#), tinggal di-deserialize. Dalam hal ini saya pake data JSON tersebut pada event CallBack Janus Grid:

protected void GridEX1_CustomCallBack(object sender, Janus.Web.Common.CustomCallBackEventArgs e) { System.Web.Script.Serialization.JavaScriptSerializer jsSerializer = new System.Web.Script.Serialization.JavaScriptSerializer(); var edata = System.Web.HttpUtility.UrlDecode(e.EventData.ToString()); var data = jsSerializer.DeserializeObject(edata); // data selanjutnya di prosess di sini }

Simple banget.

Hasil deserialize object ketika debugging tersebut dapat dilihat seperti gambar di bawah.

image

Dari gambar tersebut, string JSON telah di-deserialize menjadi object (string, Date, dll). Mudah bukan?

Semoga bermanfaat.


Categories: ASP.NET AJAX | JavaScript | JSON | Tips
Permalink | Comments (4) | Post RSSRSS comment feed

Comments

komara Indonesia | Reply

Saturday, January 03, 2009 6:08 PM

komara

infonya bagus banget walaupun saya msh bljr..

California Health Insurance United States | Reply

Thursday, February 05, 2009 2:54 PM

California Health Insurance

In ASP.NET AJAX Extensions v1.0 for ASP.NET 2.0 there is the JavaScriptSerializer class that provides JSON serialization and deserialization functionality. However, in .NET 3.5 the JavaScriptSerializer has been marked obsolete. The new object to use for JSON serialization in .NET 3.5 is the DataContractJsonSerliaizer object. I'm still new to the DataContractJsonSerializer, but here's a summary of what I've learned so far...

Married Chat United States | Reply

Sunday, February 08, 2009 5:32 PM

Married Chat

Can anyone point me to some JSON serialize / deserialize how to's? I know I ran across some examples earlier last week but I can't remember where I found it.

Birch Tire and Auto Service United Kingdom | Reply

Tuesday, February 10, 2009 2:55 AM

Birch Tire and Auto Service

JSON (JavaScript Object Notation) is an efficient data encoding format that enables fast exchanges of small amounts of data between client browsers.

http://www.birchtireautoservice.com/
Birch Tire has provided 3 generations of automobile service at our location on Route 46 in Rockaway, NJ.

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading