//方法一:直接返回序列化后的json文件 public static HttpResponseMessage ConvertToJson(this Object obj) { String str=""; if (obj is String || obj is Char) { str = obj.ToString(); } else { string json = Newtonsoft.Json.JsonConvert.SerializeObject(obj); } HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(str, Encoding.GetEncoding("UTF-8"), "application/json") }; return result; }
//方法二:global.asax.cs文件中加上 HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(str, Encoding.GetEncoding("UTF-8"), "application/json") };
时间: 2024-11-05 11:57:04