首页 > 技术文章 > C#将JSON文本转换成HttpResponseMessage数据行

topsyuan 2019-10-17 10:06 原文

  

/// <summary>
/// 将JSON文本转换成HttpResponseMessage数据行
/// </summary>
/// <param name="HttpResponseMessage文本">HttpResponseMessage文本</param>

public static HttpResponseMessage toJson1(object obj) {
        string str;
        if (obj is String || obj is Char)
        {
            str = obj.ToString();
        }
        else
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();//     为启用 AFAX 的应用程序提供序列化和反序列化功能。
            str = serializer.Serialize(obj);//将对象转换成JSON字符串
        }
        HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(str, Encoding.GetEncoding("UTF-8"), "application/json") };
        return result;
    }

 

推荐阅读