我正在使用ASP.NET开发WEB API。我在序列化数据表时遇到了一些问题。当我序列化DataTable时,它被序列化为Json字符串。但我需要JSON对象或JSON数组。
我的代码
[ActionName("EMPID")]
public System.Collections.IEnumerable Get()
{
SqlConnection myConnection = new SqlConnection();
string connString = ConfigurationManager
.ConnectionStrings["DefaultConnection"]
.ConnectionString;
myConnection.ConnectionString = connString;
DataTable dataTable = new DataTable();
string query = "select EMPLOYEE_ID,FIRST_NAME,SALARY from Employee ";
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dataTable);
var JSONString = JsonConvert.SerializeObject(dataTable);
return JSONString;
}
public class Employee
{
public int EmployeeId { get; set; }
public string Name { get; set; }
public int ManagerId { get; set; }
}
但它以JSON字符串的形式返回。我需要作为JSON对象。
示例
"[{\"EMPLOYEE_ID\":2,\"FIRST_NAME\":\"Michael",\"SALARY":800000},{\"EMPLOYEE_ID\":3,\"FIRST_NAME \":\"Roy \",\"SALARY \":700000},{\"EMPLOYEE_ID\":6,\"FIRST_NAME\":\"Philip\",\"SALARY \":7500000},{\"EMPLOYEE_ID\":7,\"FIRST_NAME \":\"Sachin\",\"SALARY":1100000}EMPLOYEE_ID\":8,\"FIRST_NAME\":\"Anshu\",\"SALARY":1200000},{\"EMPLOYEE_ID\":9,\"FIRST_NAME":\"Ravish",\"SALARY":1000000}]">
但我需要序列化为类对象,
{"姓名":"玛纳斯","性别":"男性","生日":"1987-8-8"}
我对JSON序列化有点困惑;反序列化。
1)停止使用DataTable。它们对数据的抽象性很差。使用强类型对象。Dapper很方便。它是一个可与任何ADO.NET数据提供程序配合使用的微ORM。您提供查询,它会将查询结果映射到强类型对象(类)。Dapper在NuGet上可用。
2) 您的操作方法不应返回IEnumerable
。它应该返回一个IEnumerable<T>
3) 您应该让框架处理将对象转换为JSON的操作,而不是自己操作。不需要涉及JSON.NET。如果您从操作方法返回对象,框架将为您将其转换为JSON。
4) 您没有在一次性对象(SqlConnection)上正确使用IDisposable模式。它们需要封装在using
语句中或在finally
块中处理。
5) 您有两个SqlConnection,只需要一个。
6) 您应该使用存储库模式,而不是直接在控制器中进行数据访问。按照关注点的分离,让一个单独的类负责这一点。
数据存储库
using Dapper; //add this to your using statements
public class EmployeeRepository
{
private readonly string _connectionString;
public EmployeeRepository(string connectionString)
{
_connectionString = connectionString;
}
public List<Employee> GetAllEmployees()
{
string query = "select EMPLOYEE_ID, FIRST_NAME, SALARY from Employee";
using (SqlConnection connection = new SqlConnection(_connectionString))
{
// Query is an extension method from Dapper
List<Employee> employees = connection.Query<Employee>(query).AsList();
return employees;
}
}
}
控制器
public class EmployeeController
{
private readonly EmployeeRepository _employeeRepository;
public EmployeeController()
{
string connString = ConfigurationManager
.ConnectionStrings["DefaultConnection"]
.ConnectionString;
_employeeRepository = new EmployeeRepository(connString);
}
[ActionName("EMPID")] //why does this say EMPID?
public IEnumerable<Employee> Get()
{
List<Employee> employees = _employeeRepository.GetAllEmployees();
return employees;
}
}
型号
public class Employee
{
public int EmployeeId { get; set; }
public string FirstName { get; set; }
public int Salary { get; set; }
}
如果您不想使用Dapper,您可以手动处理命令的结果:
var employees = new List<Employee>();
using (var connection = new SqlConnection(connectionString))
using (var command = new SqlCommand("select EMPLOYEE_ID, FIRST_NAME, SALARY from Employee", connection))
{
connection.Open();
using(var reader = command.ExecuteReader())
{
while (reader.Read())
{
Employee employee = new Employee();
employee.EmployeeId = reader.GetInt32(reader.GetOrdinal("EMPLOYEE_ID"));
employee.FirstName = reader.GetString(reader.GetOrdinal("FIRST_NAME"));
employee.Salary = reader.GetInt32(reader.GetOrdinal("SALARY"));
employees.Add(employee);
}
}
}
return employees;