我在MVC項目中開發了Web API。我在我的post方法中返回一個JToken對象。通常我的API正常工作,但在特定的數據有時我得到這個錯誤:從asp.net POST方法返回JSON時出現「Unable to translate Unicode character \ uD83C」錯誤
"Message": "An error has occurred.",
"ExceptionMessage": "The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.",
"ExceptionType": "System.InvalidOperationException",
"StackTrace": null,
"InnerException": {
"Message": "An error has occurred.",
"ExceptionMessage": "Unable to translate Unicode character \\uD83C
at index 411 to specified code page."
這是我的POST方法
public JToken Post([FromBody]Classes.Search search)
{
Classes.ReturnSearch returnSearch = new Classes.ReturnSearch();
try
{
string con = System.Configuration.ConfigurationManager.ConnectionStrings["TConnectionString"].ConnectionString;
SqlConnection cn = new SqlConnection(con);
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter("SP_Searchi", cn);
sqlDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
sqlDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@word", search.word));
sqlDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@num", search.num));
DataSet d = new DataSet();
sqlDataAdapter.Fill(d);
DataTable table = d.Tables[0];
foreach(DataRow row in table.Rows)
{
string result=row[1].ToString();
returnSearch.search_items.Add(new Classes.SearchItem(row[0].ToString(), row[1].ToString(),search.word));
}
returnSearch.status = "Success";
return JObject.Parse(JsonConvert.SerializeObject(returnSearch));
}
catch (Exception e)
{
returnSearch.status = "Failed";
returnSearch.search_items = null;
ValuesController.Log("Error in Search: "+e.Message);
return JObject.Parse(JsonConvert.SerializeObject(returnSearch));
}
}
有什麼問題?
['U + D83C'不是有效的Unicode字符](http://www.fileformat.info/info/unicode/char/d83c/index.htm)。這個角色是如何出現在你的返回對象中的? – dbc
我繼續並修改了原始標題*旋轉後的重疊循環視圖*可以更好地反映問題。如果這是不可取的,請隨時更換。 – dbc