0

I want to pass the data to javascript code from my code behind file (C#) and what I want at javascript side is something like the following

var data = [
    { 
        "date": "24-Apr-07", 
        "close": 93.24 
    }, 
    { 
        "date": "25-Apr-07", 
        "close": 95.35 
    }, 
    { 
        "date": "26-Apr-07", 
        "close": 98.84 
    }
];

I tried passing this as a string but failed since it considers the whole thing as a string only.

Is there any other option available ?

2
  • 1
    What web framework are you using? Classic ASP? ASP MVC? Commented Jul 19, 2017 at 7:52
  • asp.net web forms Commented Jul 19, 2017 at 7:52

4 Answers 4

0

You can try to use http://www.newtonsoft.com/json.

Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Sizes = new string[] { "Small" };

string json = JsonConvert.SerializeObject(product);
// {
//   "Name": "Apple",
//   "Expiry": "2008-12-28T00:00:00",
//   "Sizes": [
//     "Small"
//   ]
// }

And in your JS you can deserialize it in this way:

var des_obj = JSON.parse( json_string );
Sign up to request clarification or add additional context in comments.

1 Comment

You can also try this solution: stackoverflow.com/questions/7846333/…
0

You can do it by Ajax call..

At Javascript side:

$.ajax({
        type: "POST",
        url: "Default.aspx/GetData",
        data: dataValue,                
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert("Request: " + XMLHttpRequest.toString() + "\n\nStatus: " + textStatus + "\n\nError: " + errorThrown);
        },
        success: function (result) {
            alert("We returned: " + result);
        }
    });

At Code behind:

[WebMethod]
public static string GetData()
{
   // return your data
} 

When you return object from GetData() it automatically serialize the data. You can also use return JsonConvert.SerializeObject(obj);

Comments

0

Yes, you can do that using JsonConvert Class:

In C# code behind:

C# ModelClass:

public class RootObject
    {
        public string date { get; set; }
        public double close { get; set; }
    }

create a method for return json by giving C# object like this:

public class JSONConverter
    {
        public string Convert(List<RootObject> list)
        {
            return JsonConvert.SerializeObject(list);
        }
    }

In your ActionMethod:

public void ActionResult()
{

     List<RootObject> list = repo.GetList(); // i assumed that you get list 
     //from repo
    ViewBag.modelList = JsonConverter.Convert(list);
    return View()
}

After all in C# View: (NOTE: I assume that you are working in Razor view engine )

<script>
var data = @ViewBag.modelList
<script>

Your json serialized c# array can be assigned to data.

1 Comment

Question is about ASP.NET web form application not MVC
0

If you get the string at the javascript side, all you need to do is wrap the string in JSON.parse() to get your data.

var data =  JSON.parse('[
    {
        "date": "24-Apr-07",
        "close": 93.24
    }, {
        "date": "25-Apr-07",
        "close": 95.35
    }, {
        "date": "26-Apr-07",
        "close": 98.84
    }
]');

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.