30. June 2009
I was trying to use jqGrid, an excellent jQuery based grid, for building a zero postback page. It's configuration is very simple, similar to flexigrid, but has many additional features like inline editing, subgrids etc.
What jqGrid expects is a JSON response having few objects and arrays, which are mapped to columns in the grid inside the grid.base.js file, like this.
Though the grid's AJAX call is successful, data is not being bound to thegrid. The reason is, .NET 3.5 returns JSON object which is prefixed with a "d" parameter like this.
So, to map the JSON data to the grid, we have to manually parse the AJAX response and separate the "d" parameter in AJAX success callback. This applies not only to jqGrid, but also to all jQuery plugins which expect JSON response.
I was about to write more on why .Net 3.5 prefixes "d" to JSON response, how it enhances security and how to parse it easily., but incidentally, Dave ward explained the concept excellently in his latest article: http://encosia.com/2009/06/29/never-worry-about-asp-net-ajaxs-d-again
So no more worrying about .Net's "d" again :)
8. June 2009
In my previous post, I have explained how to pass complex types using jQuery and JayRock in .NET 2.0 framework. I was digging a bit into Dave’s example (using complex types to make calling services less complex) in .NET 3.5 framework, to find how easier JSON serilaization/deserialization can be made.
When a Web service class is decorated with “ScriptService” attribute, which is in the System.Web.Script.Services namespace in .NET 3.5, the web service returns data in JSON format [MSDN]. Whether the returned value is a string or an object, it will be in JSON format. So no need of writing chunks of code for serializing objects explicitly.
Therefore, all that we need to do is to simply decorate our web service class with “ScriptService” attribute and return an object from the server. So, to create a JSON object like this:
We can simply return a person object like this:
Public Function fnFetchDetailsClass() As Person
Dim objPerson As New Person
objPerson.FirstName = "Krishna"
objPerson.LastName = "Chaitanya"
objPerson.City = "Hyd"
objPerson.State = "Andhra"
objPerson.Country = "India"
Catch ex As Exception
In some situations (like in client side templating), we might need to create custom, complex JSON objects on the fly. In such situations, we can make use of .NET’s “ListDictionary” and “ArrayList” classes.
The above example can be re-written using ListDictionary as:
Public Function fnFetchDetails() As ListDictionary
Dim jsonObj As New ListDictionary
jsonObj.Item("fname") = "Krishna"
jsonObj.Item("lname") = "Chaitanya"
jsonObj.Item("city") = "Hyd"
jsonObj.Item("state") = "Andhra"
jsonObj.Item("country") = "India"
Catch ex As Exception
jsonObj.Item("Error") = "Error at server"
Thus, using JSON is made easier in .NET 3.5.
Here is a simple demo which makes things clear. (I have used jQuery for making AJAX calls.)
(Note: ListDictionary stores values in Name/Value pairs, which is useful in building JSON Object. Similarly, an ArrayList can be used to build JSON Arrays. Therefore, these two can be used instead of JsonObject and JsonArray classes of Jayrock in my post Converting ASP.NET DataTable to JSON using JayRock.)