I have these data in thhe codebehind and tried to pass it to javascript function in various formats: array of list, json string but no way to get data by a javascript var object.
Here is the last format of data in the code behind:
List<string>[] array2 = new List<string>[listLenght];
array2.Initialize();
for (int ii = 0; ii < listLenght; ii++)
{
array2[ii] = new List<string>();
array2[ii].Add(Convert.ToString(dt.Rows[ii][5]));
array2[ii].Add(Convert.ToString(dt.Rows[ii][9]));
array2[ii].Add(Convert.ToString(dt.Rows[ii][10]));
array2[ii].Add(Convert.ToString(dt.Rows[ii][11]));
}
Then tried to call javascript in this way:
string createArrayScript = string.Format("var array2 = [{0},{1},{2},{3}];",
string.Join(",",",",",", array2));
But returns an error:
FormatException was unhandled by user code.
The index (zero based) must be greater than or equal to zero and less than the size of the list of topics
This is the call to the function:
Page.ClientScript.RegisterStartupScript(this.GetType(), "registerPoiArray", createArrayScript, true);
Here is the javascript var format:
var markers = Poi;
var markers = [
{
"title": "via Andria, Roma",
"lat": 41.8902643,
"lng": 12.6638589,
"description": "fdsad"
},
{
"title": "via canosa, Roma",
"lat": 41.8838417,
"lng": 12.5438227,
"description": "fdsad"
},
{
"title": "via taranto, Milano",
"lat": 45.4383343,
"lng": 9.1505354,
"description": "fdsad"
},
{
"title": "via barletta, Roma",
"lat": 41.9102707,
"lng": 12.4580826,
"description": "fdsad"
}];
I can not pass this array in javascript.
I have these data in thhe codebehind and tried to pass it to javascript function in various formats: array of list, json string but no way to get data by a javascript var object.
Here is the last format of data in the code behind:
List<string>[] array2 = new List<string>[listLenght];
array2.Initialize();
for (int ii = 0; ii < listLenght; ii++)
{
array2[ii] = new List<string>();
array2[ii].Add(Convert.ToString(dt.Rows[ii][5]));
array2[ii].Add(Convert.ToString(dt.Rows[ii][9]));
array2[ii].Add(Convert.ToString(dt.Rows[ii][10]));
array2[ii].Add(Convert.ToString(dt.Rows[ii][11]));
}
Then tried to call javascript in this way:
string createArrayScript = string.Format("var array2 = [{0},{1},{2},{3}];",
string.Join(",",",",",", array2));
But returns an error:
FormatException was unhandled by user code.
The index (zero based) must be greater than or equal to zero and less than the size of the list of topics
This is the call to the function:
Page.ClientScript.RegisterStartupScript(this.GetType(), "registerPoiArray", createArrayScript, true);
Here is the javascript var format:
var markers = Poi;
var markers = [
{
"title": "via Andria, Roma",
"lat": 41.8902643,
"lng": 12.6638589,
"description": "fdsad"
},
{
"title": "via canosa, Roma",
"lat": 41.8838417,
"lng": 12.5438227,
"description": "fdsad"
},
{
"title": "via taranto, Milano",
"lat": 45.4383343,
"lng": 9.1505354,
"description": "fdsad"
},
{
"title": "via barletta, Roma",
"lat": 41.9102707,
"lng": 12.4580826,
"description": "fdsad"
}];
I can not pass this array in javascript.
Share Improve this question edited Jan 19, 2023 at 13:51 Hille 2,3191 gold badge27 silver badges42 bronze badges asked Oct 24, 2013 at 13:54 undertreeundertree 791 gold badge2 silver badges10 bronze badges 1- Serialize your datatable to Json – Pranav Singh Commented Oct 24, 2013 at 13:58
5 Answers
Reset to default 3You can create a custom class to represent the Datatable. Say if your data table has four columns, create a custom class which has 4 fields in it. Loop through the data table and convert it in to an array of objects of the custom class type.
Finally you can serialize the array into json using the following code.
JavaScriptSerializer js = new JavaScriptSerializer();
js.Serialize(data);
where data is the array of objects.
This is the technique i used..
Data table cannot be serialized easily to JSON directly. refer What's the best way to jSON serialize a .NET DataTable in WCF?
You have to escape (double up) the curly brace when using String.Format if you want it to spit out the actual curly brace character.
Example:
string createArrayScript =
string.Format("var array2 = [{{0}},{{1}},{{2}},{{3}}];",
...
Try to use
System.Web.Helpers.Json.Encode
And change the structure you are getting.
List<object> toEncode=new List<object>();
foreach (DataRow dr in dt.Rows){
Dictionary<string,string> element=new Dictionary<string,string>();
element.Add("title",dr["title"] as string);
//repeat for all neaded colums
toEncode.Add(element);
}
string jsonString=Json.Encode(toEncode);
I created a gist when I put the solution.
I hope this works for you...
Regards,
Just use NewtonSoft Json and call:
var JsonString = JsonConvert.SerializeObject(NameofDT, Formatting.Indented)