注册 登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

飞天心宏的博客

依稀旧梦似曾见,相逢只恨缘太迟

 
 
 

日志

 
 
关于我

出身数学,爱好文学,从事软件开发工作。一个阳光、幽默、热爱生活的男孩子,在追求理想的路上风雨无阻,勇往直前,崇尚“梦想有多远,我们就走多远”!

统一序列化为JSON对象  

2011-02-13 17:28:13|  分类: C# 扩展 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

 /*

    * Copyright: Copyright: ?Twilight2010

    * Author: xuzhihong

    * Create date: 2010-4-22

    * Description: 统一序列化为json对象

    * 需用引用命名空间:using System.Web.Script.Serialization;

    */

    public partial class JSSerialize

    {

        /// <summary>

        /// 泛型序列化静态方法

        /// </summary>

        /// <typeparam name="T"></typeparam>

        /// <param name="t"></param>

        /// <returns></returns>

        public static string Serialize<T>(T t)

        {

            JavaScriptSerializer jss = new JavaScriptSerializer();

            string str = "";

            try

            {

                str = jss.Serialize(t);

            }

            catch (System.Exception ex)

            {

                if (t is GSReturn)

                {

                    GSReturn gsr = t as GSReturn;

                    gsr.data = null;

                    gsr.errors = "数据序列化错误:" + ex.Message;

                    str = jss.Serialize(gsr);

                }

                else if (t is TwiReturn)

                {

                    TwiReturn twi = t as TwiReturn;

                    twi.data = null;

                    twi.errors = "数据序列化错误:" + ex.Message;

                }

                else

                {

                    str ="数据序列化错误:"+ex.Message;

                }

            }

            return str;

        }

 

        public static string SerializeList<T>(List<T> list)

        {

            JavaScriptSerializer serializer = new JavaScriptSerializer();

            string strJson = serializer.Serialize(list);

            strJson = strJson.Substring(1);

            strJson = strJson.Insert(0, "{totalCount:" + list.Count + ",data:[");

            strJson += "}";

 

            return strJson;

        }

 

        public static string SerializeList<T>(List<T> list,int? totalCount)

        {

            JavaScriptSerializer serializer = new JavaScriptSerializer();

            string strJson = serializer.Serialize(list);

            strJson = strJson.Substring(1);

            strJson = strJson.Insert(0, "{totalCount:" + totalCount + ",data:[");

            strJson += "}";

 

            return strJson;

        }

 

        public static string SerializeList<T>(List<T> list, int totalCount)

        {

            JavaScriptSerializer serializer = new JavaScriptSerializer();

            string strJson = serializer.Serialize(list);

            strJson = strJson.Substring(1);

            strJson = strJson.Insert(0, "{totalCount:" + totalCount + ",data:[");

            strJson += "}";

 

            return strJson;

        }

 

        public static string SerializeIList<T>(IList<T> list)

        {

            JavaScriptSerializer serializer = new JavaScriptSerializer();

            string strJson = serializer.Serialize(list);

            strJson = strJson.Substring(1);

            strJson = strJson.Insert(0, "{totalCount:" + list.Count + ",data:[");

            strJson += "}";

 

            return strJson;

        }

 

        /// <summary>

        /// 泛型 反序列化静态方法

        /// </summary>

        /// <typeparam name="T"></typeparam>

        /// <param name="str"></param>

        /// <returns></returns>

        public static T Deserialize<T>(string str)

        {

            JavaScriptSerializer jss = new JavaScriptSerializer();

            return jss.Deserialize<T>(str);

        }

 

    }

  评论这张
 
阅读(1264)| 评论(2)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018