问题描述
鉴于记录中的 utf8 字段,是否有任何简单的方法来处理序列化问题?
另外,我使用的是基于soap的旧ASMX Web API,我想以JSON的形式返回响应。
这是假设处理序列化的代码示例。请注意,我使用的是 Newtonsoft.Json
库
我收到乱码字段的问题
public static DataTable GetVlue(string sprQuery,int Entry)
{
DataTable table = new DataTable();
using (var conn = new sqlConnection(ConfigurationManager.ConnectionStrings["somecon"].ConnectionString))
using (var command = new sqlCommand(sprQuery,conn))
{
conn.open();
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("@entry",Entry);
using (sqlDataAdapter da = new sqlDataAdapter(command))
{
da.Fill(table);
return table;
}
}
}
** 主要功能部分**
table = DbService.GetVlue(storedProcedurequery,Entry);
JSONString = JsonConvert.SerializeObject(table);
Context.Response.Write(JSONString);
解决方法
所以解决问题的方法是将响应字符集定义为 UTF
我必须添加以下代码段来解决给定的问题 Context.Response.Charset = "utf-8"