Winsmarts.com

Microsoft MVP

MVP Logo

Awarded the Microsoft MVP Award.

Hosted By

blah!bLaH!BLOG!!

How to parse JSON from C#

Posted on 12/15/2009 @ 12:35 AM in #WCF | 2 comments | 2346 views

WCF is VERY VERY elegant!! If you’re unfamiliar with WCF, boy are you behind times! No really, I mean it! Stop whatever you’re doing and learn WCF. This is a technology that every .NET dev will be required to know. Please see What is WCF? for a basic introduction on WCF. Followed by writing a simple Hello World WCF service, a WCF Client, and hosting such a service!

Also, I had pioneered the work of properly making WCF work in SharePoint 2007. Well, the good news is SharePoint 2010 has wholly embraced WCF. I mean, in every single respect. SharePoint 2010 has WCF flowing through it’s veins!!! I mean, the service architecture, client object model, ADO.NET data services, REST API, heck every damn thing in SP2010 uses WCF. .. I told you .. shudda learnt it :-). Anyway, still not too late! And my book will cover it well enough too.

WCF is cool because it is very very versatile. It is very well architected. One of the things WCF comes with is very good support for very thin clients, i.e. JavaScript, JSON etc.

Well y’know, JSON is awesome for many reasons too. Why I like JSON?

  1. It’s lightweight, quick and easy to transfer
  2. It is damn easy to parse in JavaScript
  3. It serializes and deserializes without messing up your HTML or such text with escapable characters
  4. It works beautifully with jQuery

Now it is pretty damn easy to parse in JavaScript, but did you know that WCF has pretty good support for deserializing JSON as well? I mean, right in C#!

Assuming that your WCF service looked like this –>

  1: [ServiceContract]
  2: public interface IMyWCFContract
  3: {
  4:     [OperationContract]
  5:     [WebGet(UriTemplate = "OperationName?parameterID={parameterID}", ResponseFormat = WebMessageFormat.Json)]
  6:     BusinessObjectType SomeCoolOperation(int parameterID);
  7: }

 

You could deserialize it into a strongly typed .NET object as shown below:

 

  1: HttpWebRequest webRequest = WebRequest.Create(serviceUrl) as HttpWebRequest;
  2: HttpWebResponse webResponse = webRequest.GetResponse() as HttpWebResponse;
  3: Encoding enc = Encoding.GetEncoding(1252);
  4: StreamReader configStream = new StreamReader(webResponse.GetResponseStream(), enc);
  5: string configuration = configStream.ReadToEnd();
  6: JavaScriptSerializer jSerialize = new JavaScriptSerializer();
  7: BusinessObjectType businessObject = jSerialize.Deserialize<BusinessObjectType>(configuration);
 
The above code will give you a strongly typed instance of BusinessObjectType in the client code!
Keep being cool!

On 12/15/2009 10:53:39 PM Fred Morrison said ..
You might want to augment this article with an actual BusinessObject, stressing the need to decorate each of the properties you want JSON'ized with [DataMember] and [ScriptIgnore] on the ones you don't want JSON'ized, like a DataSet. I actually ran into that issue with some legacy code that used an entity that included a DataSet property named Metadata. I couldn't get it to JSON'ize until I put [ScriptIgnore] on that property (which lucky for me, wasn't missed on the other end, a piece of javascript).


On 12/16/2009 12:45:16 AM Sahil Malik said ..
Fred - Excellent feedback! Y'know I'm exactly that in a particular project right now!

Please post your comments:


Your feedback will be submitted for moderation, and will appear after it is approved.

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 

Site designed and maintained by Sahil Malik | All Rights Reserved. ©2007 WinSmarts.com.