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

Deserialize JSON String ke Strong-Type Object.

Tulisan ini merupakan kelanjutan dari post sebelumnya tentang Json Serializer dan Deserializer. Pada tulisan sebelumnya, string JSON dideserialize ke dalam Generic Dictionary. Kekurangan dari metode tersebut adalah kita tidak bisa menggunakan strong-type object untuk deserialize JSON. Metode tersebut juga sudah dianggap usang oleh .NET Framework 3.5 karena dalam framework tersebut sudah disediakan pengganti System.Web.Script.Serialization.JavaScriptSerializer yaitu System.Runtime.Serialization.Json.DataContractJsonSerializer.

Untuk dapat menggunakan System.Runtime.Serialization.Json.DataContractJsonSerializer perlu menambahkan dua assembly reference ke dalam project yaitu: System.Runtime.Serialization (butuh .NET Framework 3.0) dan System.ServiceModel.Web (butuh .NET Framework 3.5). Setelah kedua assembly ini direferensikan, beberapa hal yang perlu disiapkan diantaranya.

1. Tambahkan kedua namespace berikut

using System.Runtime.Serialization.Json; using System.Runtime.Serialization;

2. Buat class yang digunakan untuk men-deserialize JSON string.

Dalam contoh ini dibuat class berdasarkan JSON yang sama seperti tulisan sebelumnya (Json Serializer dan Deserializer). Struktur string JSON tersebut adalah:

var dateFormat = '<%= GlobalCallCenter.Common.CallCenterConfig.Get("SHORT_DATE_FORMAT") %>'; 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 };


Dari JSON tersebut di atas, dibuat class sebagai berikut:

[DataContract(Name = "Filter", Namespace = "")] protected internal class Filter { [DataMember(Name = "instance", Order = 1)] public string Instance { get; set; } [DataMember(Name = "requestNo", Order = 2)] public string RequestNo { get; set; } [DataMember(Name = "category", Order = 3)] public string Category { get; set; } [DataMember(Name = "itemCategory", Order = 4)] public string ItemCategory { get; set; } [DataMember(Name = "ticketGroup", Order = 5)] public string TicketGroup { get; set; } [DataMember(Name = "status", Order = 6)] public string Status { get; set; } [DataMember(Name = "requestType", Order = 7)] public string RequestType { get; set; } [DataMember(Name = "startDate", Order = 9)] public DateTime StartDate { get; set; } [DataMember(Name = "finishDate", Order = 10)] public DateTime FinishDate { get; set; } [DataMember(Name = "requestedFor", Order = 10)] public string RequestedFor { get; set; } [DataMember(Name = "assigneeId", Order = 10)] public string AssigneeId { get; set; } }

3. Buat implementasi deserialization.

Seperti pada tulisan sebelumnya di Json Serializer dan Deserializer, saya gunakan contoh yang sama yaitu event callback pada Janus Grid sebagai berikut.

protected void GridEX1_CustomCallBack(object sender, Janus.Web.Common.CustomCallBackEventArgs e) { var edata = System.Web.HttpUtility.UrlDecode(e.EventData.ToString()); System.IO.MemoryStream stream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(edata.ToString())); DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Filter)); Filter filter = (Filter)serializer.ReadObject(stream); }

Dengan menggunakan strong-type kita langsung dapat mengakses property dari class tersebut, seperti terlihat pada gambar berikut.

image

Berbeda dari tulisan sebelumnya, dengan menggunakan strong-type object, semua property dapat diakses sesuai dengan tipe yang sudah didefinisikan.

Semoga bermanfaat.


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

Comments

technology Indonesia | Reply

Monday, July 14, 2008 10:40 AM

technology

mas ... untuk membuat file Json apa sih yang dibutuhkan?
untuk parsing Json menggunakan ajax dah dibahas blom?

Ahmad Masykur Indonesia | Reply

Monday, July 14, 2008 11:06 AM

Ahmad Masykur

Yang dibutuhkan hanya JavaScript.
Untuk parsing JSON silakan baca:
www.masykur.web.id/post/Parsing-Data-JSON.aspx
www.masykur.web.id/.../...a-JSON-melalui-AJAX.aspx

captain naruto Indonesia | Reply

Tuesday, September 16, 2008 3:09 PM

captain naruto

om masykur ... mau nanya neh, gimana sih cara aggregating isi web ke RSS (prinsipnya sama ngak dengan yang dipake JSON)

yud Indonesia | Reply

Wednesday, October 29, 2008 2:06 PM

yud

halo mas. kmren ktmu di MSDN Day.
saya mo tanya, klo parse string json (yang dikirim javascript via ajax) menjadi sesuatu (object probably) di asp classic bagaimana ya? saya cari2 di internet kok susah banget. kebanyakan sebaliknya, membahas parsing dari asp ke json javascript.
terimakasih.

Web Design Leicester United Kingdom | Reply

Saturday, January 24, 2009 11:54 PM

Web Design Leicester

For some years the Internet has been dominated by phrases like Web 2.0 and Ajax. The catchword Web 2.0, which was originally established by O’Reilly at the first Web 2.0 conference in October 2004, not only describes a new way of perception and usage of the internet (e.g. social software like blogs, wikis, etc.), but also stands for more or less innovative techniques as for instance RSS or Ajax. The latter is a combination of techniques that have been available since the late 1990s, such as JavaScript, asynchronous requests and XML. However, the term Ajax only exists since Jesse James Garret introduced it in his article in February 2005. Since then Ajax has experienced a real hype. Google Mail, Google Maps or Flickr just serve as examples for the mass of applications that have to attribute their success substantially to Ajax. ...

gogi juice United States | Reply

Friday, January 30, 2009 12:54 AM

gogi juice

The term "Web 2.0" describes the changing trends in the use of World Wide Web technology and web design that aim to enhance creativity, communications, secure information sharing, collaboration and functionality of the web. Web 2.0 concepts have led to the development and evolution of web-culture communities and hosted services, such as social-networking sites, video sharing sites, wikis, blogs, and folksonomies. The term first became notable after the O'Reilly Media Web 2.0 conference in 2004.

Precision Auto Services, Morris NJ United Kingdom | Reply

Thursday, February 05, 2009 9:36 PM

Precision Auto Services, Morris NJ

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.


http://www.carserviceautorepair.com/
Experienced, certified mechanics for fine European cars and their owners.  

Birch Tire and Auto Service United Kingdom | Reply

Tuesday, February 10, 2009 2:55 AM

Birch Tire and Auto Service

.. Saya ingin berbagi sedikit tips mengenai JSON serializer dan deserializer yang ada di ... Deserialize-JSON-String-ke-Strong-Type-Object.aspx ...

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