1

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.

Hille
  • 2,123
  • 22
  • 39
undertree
  • 79
  • 1
  • 1
  • 10

5 Answers5

3

You 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?

Community
  • 1
  • 1
Saranya
  • 1,988
  • 16
  • 20
1

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}}];",
     ...
nothingisnecessary
  • 6,099
  • 36
  • 60
0

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);
unconnected
  • 991
  • 1
  • 10
  • 21
0

I created a gist when I put the solution.

I hope this works for you...

Regards,

Augusto Pedraza
  • 538
  • 3
  • 18
0

Just use NewtonSoft Json and call:

var JsonString = JsonConvert.SerializeObject(NameofDT, Formatting.Indented)
MattE
  • 1,044
  • 1
  • 14
  • 34