首页 > Web开发 > 详细

将对象转为json,加入到HttpResponseMessage中

时间:2016-03-16 12:03:37      阅读:614      评论:0      收藏:0      [点我收藏+]

需要引用程序集:

System.Net.Http
System.Web
System.Web.Extensions

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Web;
using System.Web.Script.Serialization;

namespace Common.Utils
{
    public class ToJsonTran
    {
        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;
        }
    }
}

  

将对象转为json,加入到HttpResponseMessage中

原文:http://www.cnblogs.com/xsj1989/p/5282739.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!