C# 序列化与反序列化

前台拼json,json 格式

// [{}, {}, {}]

//{ 
// "DoctorID": "1635a62e-eaea-499d-8d12-94ecd256ebdc",
// "DoctorName": "林心如",
// "DepartmentID": "CC209858-04AE-45AC-A48C-B07EC36EC012",
// "DepartmentName": "心外科",
//}
var jsonStr = "";
var json = "[";
var DepartmentID, DepartmentName, DoctorID, DoctorName;
for (var i = 0; i < table.rows.length; i++) {
if (i != 0) {
var cellsInfo = table.rows[i].cells;
for (var j = 0 ; j < cellsInfo.length; j++) {
switch (j) {
case 0:
DepartmentName = cellsInfo[j].innerText;
break;
case 1:
DepartmentID = cellsInfo[j].innerText;
break;
case 3:
DoctorName = cellsInfo[j].innerText;
break;
case 4:
DoctorID = cellsInfo[j].innerText;
break;
default:
}
}
jsonStr = '{"DepartmentName":"' + DepartmentName + '","DepartmentID":"' + DepartmentID + '","DoctorName":"' + DoctorName + '","DoctorID":"' + DoctorID + '"}';
json += jsonStr + ",";
}
}

json = json.substring(0, json.length - 1) + "]";
//console.log(json);
var data = {requestJson:json};
$.ajax({
type: "post",
url: '/SelectDoctor/SelectResult',
dataType: "json",
cache: false,
data: data,
error: function () { 
},
success: function (ret) { 
}
});
});

后台反序列化:

public void SelectResult()
{

string json = Request.Form["requestJson"];

//此方法可以直接把json反序列化成 对象的集合

List<SelectDoctorModel> sdm= jss.Deserialize<List<SelectDoctorModel>>(json);

}

参考文档:

http://www.cnblogs.com/dwfbenben/archive/2013/06/06/3122662.html

原文地址:https://www.cnblogs.com/wei-lai/p/5089354.html