我在web方法中传递多维数组作为对象类型。我想在方法中将此对象类型转换为多个数组。
执行转换的方法:
[WebMethod]
public static string Save(object arr)
{
Dictionary<string, object> value = (Dictionary<string, object>)arr;
return "";
}
客户端代码:
//ajax method
var arr=new Array();
var table = document.getElementById('table');
for (var r = 0, n = table.rows.length; r < n; r++) {
arr[r] = new Array(10);
for (var c = 0, m = table.rows[r].cells.length; c < m; c++) {
arr[r][c]=table.rows[r].cells[c].innerHTML;
}
}
console.log(arr);
$.ajax({
type: "POST",
dataType: "json",
contentType: "application/json",
url: "salary.aspx/Save",
data: JSON.stringify({ arr: arr }),
success: function (response) {
alert(response.d);
}
});
错误消息是:
无法将类型为“System.Object []”的对象强制转换为类型 'System.Collections.Generic.Dictionary`2 [System.Object的,System.Object的]'。
答案 0 :(得分:1)
首先,您需要以不同方式创建内部数组:
//ajax method
var arr=new Array();
var table = document.getElementById('table');
for (var r = 0, n = table.rows.length; r < n; r++) {
arr[r] = {};
for (var c = 0, m = table.rows[r].cells.length; c < m; c++) {
arr[r][c]=table.rows[r].cells[c].innerHTML;
}
}
console.log(arr);
$.ajax({
type: "POST",
dataType: "json",
contentType: "application/json",
url: "salary.aspx/Save",
data: JSON.stringify({ arr: arr }),
success: function (response) {
alert(response.d);
}
});
然后你应该可以做这样的事情:
[WebMethod]
public static string Save(object arr)
{
object[] table = (object[])arr;
// now the the object contains all your row values as an object.
return "";
}