我知道这是ASP.net中最常见的问题之一,但我的情况有点不同。
我有一个json对象,我将其转换为字符串并通过jquery ajax将其传递给web方法。
create table table1 (id number(2), date_t1 date, username varchar2(5), password varchar2(8));
create table table2 (id number(2), date_t1 date, username varchar2(5), city varchar2(8));
insert into table1 values (1, to_date('16.02.2016', 'dd.mm.yyyy'), 'XYZ', 'ABC123');
insert into table1 values (2, to_date('16.02.2016', 'dd.mm.yyyy'), 'ABC', 'XYZ123');
insert into table2 values (1, to_date('16.02.2016', 'dd.mm.yyyy'), 'XYZ', 'NYC');
insert into table2 values (2, to_date('16.02.2016', 'dd.mm.yyyy'), 'ABC', 'LA');
declare
n_id number(2);
d_date date;
v_username varchar2(5);
v_password varchar2(8);
n_check number(1);
begin
n_check := 0;
-- fill the variables with data which you want to insert:
n_id := 2;
d_date := to_date('16.02.2016', 'dd.mm.yyyy');
v_username := 'ABC';
v_password := 'CCCCC';
-- check whether an entry exists in table2:
begin
select count(1)
into n_check
from table2 t2
where t2.id = n_id
and trunc(t2.date_t1) = trunc(d_date);
exception when no_data_found then n_check := 0;
end;
-- if an entry exists in table2, then insert into table1:
if n_check <> 0 then
insert into table1 (id, date_t1, username, password)
values (n_id, d_date, v_username, v_password);
end if;
end;
/
select * from table1;
select * from table2;
delete from table1;
delete from table2;
现在当我在web方法中收到它时,会以对象的形式出现。
function Save() {
var content = $('#radGridSMT_ctl00 tr').not('#radGridSMT_ctl00 thead tr');
var header = $('#radGridSMT_ctl00_Header thead tr');
var result = [];
for (var i = 0; i < content.length - 1; i++) {
item = {}
var headerName;
item[header[0].children[0].textContent] = content[i].children[0].textContent;
item[header[0].children[1].textContent] = content[i].children[1].textContent;
item[header[0].children[2].textContent] = content[i].children[2].textContent;
item[header[0].children[3].textContent] = content[i].children[3].textContent;
item[header[0].children[4].textContent] = content[i].children[4].textContent;
item[header[0].children[5].textContent] = content[i].children[5].textContent;
item[header[0].children[6].textContent] = content[i].children[6].textContent;
item[header[0].children[7].textContent] = content[i].children[7].textContent;
item[header[0].children[8].textContent] = content[i].children[8].textContent;
item[header[0].children[9].textContent] = content[i].children[9].textContent;
item[header[0].children[10].textContent] = content[i].children[10].textContent;
item[header[0].children[11].textContent] = content[i].children[11].textContent;
item[header[0].children[12].textContent] = content[i].children[12].textContent;
result.push(item);
}
result = JSON.stringify({ data: result });
$.ajax({
type: "POST",
url: "SMTRequiredHour.aspx/SaveData",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: result,
success: function (response) {
},
failure: function (response) {
}
});
}
JsonConvert.DeserializeObject接受一个字符串而不是一个对象,这就是问题所在。我无法将JSON对象转换为字符串。
我无法创建强类型对象,因为json对象中的列是动态生成的,每次都可能不一样。
如何将JSON对象解析为字符串,以便我可以对其进行反序列化。
答案 0 :(得分:0)
data.ToString()
返回什么?
如果它没有解决它,请尝试从JavaScript端将其作为文本发送。您可以使用data: JSON.stringify(result),
然后在Web方法中将数据类型设置为字符串。