我试图使用 JavaScriptSerializer 将 linq 结果转换为 JSON,但我没有得到所需的输出。
我的代码是(我使用 C#):
string Departments = "";
JavaScriptSerializer jss = new JavaScriptSerializer();
Departments = jss.Serialize(db.Departments.ToList());
ViewBag.Department = Departments;
我得到的是:
[{"DepartmentId":2,"DepartmentName":"Cardiology","EntityState":2,"EntityKey":{"EntitySetName":"Departments","EntityContainerName":"ClinicDbEntities","EntityKeyValues":[{"Key":"DepartmentId","Value":2}],"IsTemporary":false}},{"DepartmentId":6,"DepartmentName":"ENT","EntityState":2,"EntityKey":{"EntitySetName":"Departments","EntityContainerName":"ClinicDbEntities","EntityKeyValues":[{"Key":"DepartmentId","Value":6}],"IsTemporary":false}},{"DepartmentId":7,"DepartmentName":"Paediatrics","EntityState":2,"EntityKey":{"EntitySetName":"Departments","EntityContainerName":"ClinicDbEntities","EntityKeyValues":[{"Key":"DepartmentId","Value":7}],"IsTemporary":false}},{"DepartmentId":8,"DepartmentName":"General","EntityState":2,"EntityKey":{"EntitySetName":"Departments","EntityContainerName":"ClinicDbEntities","EntityKeyValues":[{"Key":"DepartmentId","Value":8}],"IsTemporary":false}}]
我需要的是这样的:
[ { DepartmentId: 2, DepartmentName: "Cardiology" }, { DepartmentId: 6, DepartmentName: "ENT" },{ DepartmentId: 7, DepartmentName: "Paediatrics" },{ DepartmentId: 8, DepartmentName: "General"}]
有人可以帮帮我吗?先感谢您!