我的 C# 应用程序提供数据并将其发送到 WebService,如下所示:
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()
我通过 Javascript 函数中的 WebService 检索此数据的方式如下:
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
}
现在,我需要这样的 var 数据:
var data = [['january', 2,3],['february', 3,5],['march', 5, 10]];
我需要如何将其从 C# 发送到 WebService,以便最后得到像上面那样的 var 数据?
感谢您的帮助!
最佳答案
您需要将数据作为二维数组发送。
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());
或者,更简洁地说,像这样:
var list = new List<List<string>>();
list.Add(new List<string>(new string[] { "february", "3", "5" }));
...
Service.sendList(list.ToArray());
当然,您始终可以按如下方式解析 JavaScript 中的整数:
parseInt(data[0][1], 10); // parses "2" into 2 (base 10)
关于javascript - C# 和 Javascript 传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30485054/