Saturday, December 22, 2007

Quick and Dirty JSON

So you want to write a JSON service for your Silverlight application? Basically this entire thing is a giant hack. first JSON is just the string syntax for JavaScript so it is not like a real protocol of course I get a bit of heart burn calling 'SOAP' a real protocol but really a JavaScript array defintion being a 'protocol' you must admit is a bit of a stretch but ok I can go with it.
Anyway so quick a dirty in your 'Silverlight' web site project in visual studio you can create a JSON service out of any ASPX page. First 'create' your ASPX page. then remove everything so it looks like this:

< % @ Page Language="C#" AutoEventWireup="true" CodeFile="JSON.aspx.cs" Inherits="JSON" % >
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="JSON.aspx.cs" Inherits="JSON" %>


note that 'JSON' is just what I happened to call this page.
so then just right click on the page and click view code. You'll see that you have basically one method 'Page_Load'. here basically we just need to write out whatever it is we need in the form of a javascript array. So for example here is a very simple one:

StringBuilder JSON = new StringBuilder();

string ItemDelimter = string.Empty;

JSON.Append(ItemDelimter + "\"1\" : \"john doe\" ");

ItemDelimter = " , ";

JSON.Append(ItemDelimter + "\"2\" : \"jill doe\" ");

JSON.Append(ItemDelimter + "\"3\" : \"jane doe\" ");

JSON.Append(ItemDelimter + "\"4\" : \"jared doe\" ");

JSON.Append(ItemDelimter + "\"5\" : \"jeff doe\" ");

Response.Write("[ { " + JSON.ToString() + " } ] ");

Response.End();

Note that you'll need to add System.Text for the above code but you can basically generate the out put anyway you want and accept input parameters as query string values.

3 comments:

  1. Hi David,

    Is there any special reason why you're not using the /js proxy to as standard ASMX page that comes with ASP.NET Ajax and that exposes automagically the output of your SOAP service as a JSON result?
    http://msdn.microsoft.com/msdnmag/issues/07/10/CuttingEdge/default.aspx

    Are you also aware that a JSON serializer now comes with WCF3.5?
    http://dotnetslackers.com/articles/ajax/JSON-EnabledWCFServicesInASPNET35.aspx

    Regards,
    Christophe Lauer -- http://blogs.msdn.com/clauer/

    ReplyDelete
  2. mostly because this was for a Silverlight 64bit Vista Sidebar gadget that can't use any AJAX and I didn't want to spend more then 5 minutes making it work... And only Silverlight 1.0 can be used for the project and no upgrades to the server... So I guess I should cavate this with the reason for not using ajax or something a bit more clean. The key thing was to make it work and complete the app given the circumstance as fast as possible. Hence the hacking :)

    ReplyDelete
  3. ok ok, I give. JSON is a bad idea entirely in a sidebar gadget, in vista using silverlight... it just doens't seem practical so I'm going back to some nice old fashioned SOAP/XML.

    more ranting on this topic at:

    http://hackingsilverlight.blogspot.com/2008/01/vista-sidebar-gadgets-silverlight-and.html

    ReplyDelete