Geeks With Blogs

News Welcome to Marco Anastasi and Serena Caruso's technical weblog on Microsoft .NET technologies and related matters!
Juan DoNeblo Just another Dot Net blog by marco anastasi & serena caruso

If you are a web developer, and you haven't been living under a rock for most of the past year, you must at least have heard of JSON. In the wake of the tremendous hype relating to everything even vaguely AJAX-related, JSON has climbed in record time the list of the top 10 technologies a web developer has to master.

Let's make it clear from the start: JSON does deserve a top spot in such a list, if nothing else because it's the skeleton that keeps the whole AJAX paradigm on its feet. Even more so for us .NET programmers. In fact, we'd be more accurate if we called it AJAJ, although I agree that would feel quite stupid . As a matter of fact, when using ASP.NET AJAX, most of the communication between client and server is serialized in JSON format rather than XML, at least when using the default settings. And there's good reason for that.

In fact, JSON is a very simple human-readable, easily-parsable data-interchange format, like XML, but unlike the latter, it is very concise, and, more importantly, it is immediately convertible to a Javascript object. That is, if you simply eval() a JSON expression, you get returned the actual object that was serialized in the first place.

For those of you who might not know it, eval() is a built-in javascript function that evaluates a string and executes it as a line of code (see here for more info).

For example, you might serialize a very simple "Person" object that has properties "firstName", "lastName", and "age" as follows:


Note that property names are always enclosed in double quotes, while values follow the usual javascript syntax.

In Javascript, you would then be able to use code like this:

var json = '{"firstName":"Marco", "lastName":"Anastasi", "age":27}';
var person = eval('(' + json + ')'); 

Note that we enclose the json expression we are evaluating in parentheses, to instruct eval() to treat the string as an expression rather than a block of code.

WARNING: what you see here is merely used as an example. You should avoid resorting to the eval() function in a production environment, because of potential security breaches. There are valid alternatives out there, and a good place to start looking for them is, of course

In my next posts I'll talk profusely about one of the possible solutions to this problem (and probably the one that ASP.NET developers are keener to use), i.e. ASP.NET AJAX's built-in client side JSON serializer/deserializer.

What you've seen so far is obviously a very simplified introduction to JSON, and doesn't show much of what you might achieve by leveraging its power and simplicity. In my next post, I'll delve deeper into its inner workings and how to use it to manage the communication between client side script and a server-side application, and finally to retrieve information from web services, shape it into different formats and serve it to the client browser.

For more information to get you started with JSON, visit

You can find part 2 of this article here.



Technorati Tag: , , , ,

kick it on

Posted on Monday, October 22, 2007 2:49 AM ASP.NET , Javascript | Back to top

Comments on this post: JSON in ASP.NET Ajax: First words

# re: JSON in ASP.NET Ajax: First words
Requesting Gravatar...
Could you provide an alternative to using eval in the context of this example? Is eval a bad thing to use if you are coding for a private intranet? Does it hurt performance at all?

Thanks for the article. I can't believe I made it to the end as I usually glaze over about the first paragraph when it comes to json :)
Left by Rich on Mar 10, 2009 2:52 PM

# re: JSON in ASP.NET Ajax: First words
Requesting Gravatar...
I have visited json website and its very complex I must say. Do you have any tips on websites I can visit if I am newbie?
Left by Maria on Apr 01, 2009 2:47 PM

Your comment:
 (will show your gravatar)

Copyright © Marco Anastasi & Serena Caruso | Powered by: