在做JSON示例时,我已经达到了一个我有网格视图的点,它应该绑定到数据源。数据源位于Json对象
中代码
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Employee employee = new Employee();
var list = employee.CreateEmployees();
var query = from emp in list.AsEnumerable()
select new
{
id = (int)emp.EmployeeId,
name = (string)emp.Name,
nic = (string)emp.Nic,
salary = (int)emp.Salary
};
JObject o = JObject.FromObject(new
{
Employees = query
});
Response.Write(o);
Session["JsonEmployee"] = o;
}
LoadGrid();
}
用于加载网格:
void LoadGrid() {
List<Employee> lst = new List<Employee>();
var objects = (JObject)Session["JsonEmployee"];
foreach (var v in objects)
{
lst = JsonConvert.DeserializeObject<List<Employee>>(v.Value.ToString());
}
GridView1.DataSource = lst;
GridView1.DataBind();
}
一切正常,但唯一的问题是每个员工的EmployeeId为0。
员工类如下:
public class Employee
{
public int EmployeeId { get; set; }
public string Name { get; set; }
public string Nic { get; set; }
public int Salary { get; set; }
.....
}
json对象中的注意每个员工都有正确的ID,但是当它转换为list时,为每个员工获取employeeid 0。
答案 0 :(得分:0)
在一个对象中,它被称为id,而在另一个EmployeeId中。将它们命名为相同,它应该解决问题