我正在开发一个 Web 服务,我需要将用户类型对象作为 json 响应返回。但我无法使用此代码实现它。
public class BioMatricUsers : System.Web.Services.WebService {
public BioMatricUsers()
{
}
public class my_user
{
public my_user(string i, string b, string l)
{
id = i;
badgeNumber = b;
login = l;
}
public string id { get; set; }
public string badgeNumber { get; set; }
public string login { get; set; }
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetUsers()
{
OleDbConnection co = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\confiz\Desktop\att2000.mdb");
co.Open();
OleDbCommand cmd = new OleDbCommand("SELECT * FROM USERINFO", co);
OleDbDataReader reader = cmd.ExecuteReader();
string jsonString = "";
int i = 1;
MemoryStream ms = new MemoryStream();
my_user user = null;
Type type = user.GetType();
while (reader.Read() && i <= 2)
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(user.GetType());
user = new my_user(reader["USERID"].ToString(), reader["badgenumber"].ToString(), reader["SSN"].ToString());
serializer.WriteObject(ms, user);
jsonString = Encoding.Default.GetString(ms.ToArray());
i++;
}
ms.Close();
co.Close();
return jsonString;
}
}
我在这一行收到空引用错误。DataContractJsonSerializer 序列化器 = 新 DataContractJsonSerializer(user.GetType())