public static HttpResponseMessage toJson(Object obj)
{
String str;
if (obj is String || obj is Char)
{
str = obj.ToString();
}
else
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
str = serializer.Serialize(obj);
}
HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(str, Encoding.GetEncoding("UTF-8"), "application/json") };
return result;
}转载于:https://www.cnblogs.com/smartsmile/p/6234103.html
本文介绍了一种将C#中的对象转换为JSON字符串,并将其封装为HttpResponseMessage的方法,适用于Web API中返回JSON格式的数据。
1209

被折叠的 条评论
为什么被折叠?



