简体   繁体   中英

C# and Javascript Pass DATA

My C# application provides the data and send it to a WebService, like this :

list.Add('cool'); //add value to the list
list.Add('whau'); //add value to the list
Service.sendList(list.ToArray()); //send list to the WebService called Service using the WebMethod  sendList()

and the way I retrieve this data through a WebService in a Javascript function is like this :

 WebService.getList(OnSucceeded,OnFailed);
 function OnSucceeded(result){
 var data = result[0];  //result[0] = 'cool';
 var data2 = result[1]; //result[1] = 'whau';
 }
 function OnFailed(result){
 //do nothing
 }

Now, I need my var data like this :

var data = [['january', 2,3],['february', 3,5],['march', 5, 10]];

How I need to send it from C# to the WebService in order to have at the end a var data like just above ?

Thanks for your help !

You will need to send the data as a two-dimensional array.

var list = new List<List<string>>();
list.Add(new List<string>());

list[0].Add("january");
list[0].Add("2");
list[0].Add("3");
...
Service.sendList(list.ToArray());

Or, more succinctly, like this:

var list = new List<List<string>>();
list.Add(new List<string>(new string[] { "february", "3", "5" }));
...
Service.sendList(list.ToArray());

And of course, you can always parse the integers in JavaScript as follows:

parseInt(data[0][1], 10); // parses "2" into 2 (base 10)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM