1. Back To Blog

QuestPond's interview questions and answers on JSON(JavaScript Object Notation)

Explain how to emit JSON from an ASP.NET Web Forms application?

In Web forms this can be achieved with the help of Http Handlers.

  1. In your web form project create a new Generic Http Handler. Right click the project, Add new Item >> Generic handler (file with ashx extension)

  2. In the ProcessRequest function create and return JSON string as follows

a. Create object of your .net class and initialize properties with values.

b. Create Json string of above object using JavascriptSerializer class as follows

c. Set your return type as JSON and return the above created string.

How to consume that emitted JSON later?

We can use jQuery for that. In jQuery we have some AJAX functions available using which we will be able to make a get/post request to handler created in the above example. In response we will get JavaScript object instead of JSON string.

Why the return value in above example is JavaScript object, why not it is JSON String?

$.getJSON and other AJAX functions in jQuery internally uses XMLHttpRequest object (a core JavaScript Ajax function). In reality after the AJAX call the return value was JSON string but internally jQuery converts that json string to JavaScript object and invoke the done function of it. This is why we get JavaScript object as a parameter in "call back" function passed inside "done" function.



Looking for training?

(MVC, WCF, Design Patterns, MSBI, JSON, WPF, ALM using VSTS, jQuery, HTML 5)We provide all kinds of trainings on various technologies. Visit www.JustCompile.com for detail.

Also see our video on JSON (JavaScript Object Notation) as follows: -

Shiv Prasad Koirala

Visit us @ www.questpond.com or call us at 022-66752917... read more

We are on Social