使用JSON.net 将实体类生成JSON字符串

时间:2022-11-26 18:59:01
 

使用JSON.net 生成实体类的JSON串是很简单的,比如:

//实体类

 

使用JSON.net 将实体类生成JSON字符串使用JSON.net 将实体类生成JSON字符串代码public class SysNavModel
{
        public SysNavModel()
        { }
        
        
/// <summary>
        /// 构造函数 sys_nav
        /// </summary>
        /// <param name="navid">navid</param>
        /// <param name="title">title</param>
        /// <param name="url">url</param>
        /// <param name="iconcls">iconcls</param>
        /// <param name="parentid">parentid</param>
        /// <param name="visible">visible</param>
        /// <param name="orderid">orderid</param>
        public SysNavModel(int? navid, string title, string url, string iconcls, int? parentid, bool? visible, int? orderid)
        {
            
this.navid = navid;
            
this.title = title;
            
this.url = url;
            
this.iconcls = iconcls;
            
this.parentid = parentid;
            
this.visible = visible;
            
this.orderid = orderid;
        }
        
        #region 实体属性
        private 
int? _navid;
        private string _title;
        private string _url;
        private string _iconcls;
        private 
int? _parentid;
        private bool
? _visible;
        private 
int? _orderid;
        private string _ptag;
          
        
/// <summary>
        /// navid
        /// </summary>
        public int? navid
        {
            set { _navid 
= value; }
            get { 
return _navid; }
        }

        
/// <summary>
        /// 页面标记
        /// </summary>
        public string ptag
        {
            get { 
return _ptag; }
            set { _ptag 
= value; }
        }

        
/// <summary>
        /// 导航菜单名称
        /// </summary>
        public string title
        {
            set { _title 
= value; }
            get { 
return _title; }
        }
                  
        
/// <summary>
        /// 链接地址
        /// </summary>
        public string url
        {
            set { _url 
= value; }
            get { 
return _url; }
        }
                  
        
/// <summary>
        /// ICON 样式类名
        /// </summary>
        public string iconcls
        {
            set { _iconcls 
= value; }
            get { 
return _iconcls; }
        }
                  
        
/// <summary>
        /// 上级ID
        /// </summary>
        public int? parentid
        {
            set { _parentid 
= value; }
            get { 
return _parentid; }
        }
                  
        
/// <summary>
        /// 是否显示
        /// </summary>
        public bool? visible
        {
            set { _visible 
= value; }
            get { 
return _visible; }
        }
                  
        
/// <summary>
        /// 排序ID
        /// </summary>
        public int? orderid
        {
            set { _orderid 
= value; }
            get { 
return _orderid; }
        }

        
/// <summary>
        /// 权限数组
        /// </summary>
        public ArrayList Permissions
        {
            get 
            {
                
return NavPermissionDal.Current.GetPermissionArray(this.navid.Value);
            }
        }
        #endregion
//主要代码
        public override string ToString()
        {
            StringBuilder sb 
= new StringBuilder();
            StringWriter sw 
= new StringWriter(sb);

            
new JsonSerializer().Serialize(new JsonTextWriter(sw), this);

            
return sb.ToString();
        }
}
 
重写了实体类的ToString()方法在调时 直接调用实体类的ToString() 方法就可以生JSON字符串了。简单吧 我们可以封装一个方法以便以后使用public static string ToJson(object obj)
{
   StringBuilder sb 
= new StringBuilder();
   StringWriter sw 
= new StringWriter(sb);

   JsonSerializer json 
= new JsonSerializer();
   json.Serialize(
new JsonTextWriter(sw),obj);

   
return sb.ToString();
 }