我有网格视图并将其绑定到从基于 REST 的 Web 服务调用的数据。REST 服务以 JSON 格式提供它,那么我如何反序列化并用于我的网格?
这是我拥有的大部分代码,
public ICollection<Employee> GetEmployees()
{
var request = HttpWebRequest.Create(GetEndPointAddress("GetEmployeeService"));
var response = request.GetResponse();
var stream = response.GetResponseStream();
var serializer = new DataContractSerializer(typeof(ICollection<Employee>));
var employees = serializer.ReadObject(stream) as ICollection<Employee>;
response.Close();
return employees;
}
public Uri GetServiceBaseAddress
{
get
{
return new Uri("http://localhost/Employeeservices/api");
}
}
private Uri GetEndPointAddress(string relativeAddress)
{
return new Uri(GetServiceBaseAddress.ToString() + "/" + relativeAddress);
}
我收到以下错误:
反序列化 System.Collections.Generic.ICollection`1[[EmployeeApp.Models.Employee, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] 类型的对象时出错。根级别的数据无效。线
另外,如果我对这些数据中的任何一个进行更改,如何将数据发布回服务?