How to use Json.net into the javascript

May 29, 2012 at 3:44 PM

Look at the following code first:

 public class Product
    {
        public string Name;
        public DateTime Expiry;
        public double Price;
    }

    public void CreateObject()
    {
       Product product = new Product();
        product.Name = "Apple";
        product.Expiry = new DateTime(2008, 12, 28);
        product.Price = 4.56;

        string json = JsonConvert.SerializeObject(product);
    }

/*****************************************************************************************/

I have simply created a class 'Product' and its object 'product' and converted it into the json string using 'JsonConvert.SerializeObject'.

Now i want to pass this 'json' string into the javascript.

Can any one please tell me how to do that ?

How to use json.net in the javascript ?

May 29, 2012 at 7:30 PM
Edited May 29, 2012 at 7:31 PM

You could just put this into a panel object and then on page load, call a JavaScript function which reads the contents of the DIV.

Or you could setup a AJAX request within the page, that on page load or at time t, will call another page or the same page and return the JSON using a Response.Write() and Response.End().

May 30, 2012 at 7:11 AM
Edited May 30, 2012 at 9:01 AM

Can we have anything which allows us to use json.net in javascript code.

I actually want to use it in the javascript code. I want to deserialize the json string in the javascript code.

I don't want to use the Response.Write() or .End().

Can we do anything like taking the Handler file and using  Json.Stringify() .... something like that ?

What I want to do:

I want to do the call from javascript funtion to the C# function (may be webmethod)

then it will send me a response as a json string which i will deserialize in javascript only, so please help me !

May be i will do the asynchronous call from javascript function to get the json string.