当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 组件Newtonsoft.Json实现object2json转换

ASP.NET
.Net基础:在ASP.net中网站访问量统计方法
Asp.Net 建立一个在线 RSS 新闻聚合器
ASP.NET从字符串中查找字符出现次数的方法
了解ASP.NET中的IFRAME框架挂马
JAVA和.NET两个平台对于安全功能的比较
.NET中*延迟*特性的几个陷阱
使用ASP.NET Global.asax 文件
在.NET环境下为网站增加IP过滤功能
如何实现.net程序的进程注入
在.Net Micro Framework中显示汉字
引以为戒 .NET开发者常犯的错误
WinForm程序中使用控制台作为输出窗口
浅谈如何使用 Lambda 表达式做抽象代表
.Net基础:C#中对DatagridView部分常用操作
ASP.NET LinkButton组件编程浅析
ASP.NET中使用AJAX中的方式
ASP.NET组件设计之生命周期详解
asp.net下web控件点评
.Net应用:ASP.NET中使用AJAX中的方式
.Net基础:ASP.NET中的javascript操作

ASP.NET 中的 组件Newtonsoft.Json实现object2json转换


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-28   浏览: 122 ::
收藏到网摘: n/a

很多情况下,我们需要把数据类型做一些转换,供其它外部的子系统调用。

最为典型的是生成json格式供javascript作调用。

现成的组件Newtonsoft.Json可以实现object2json之间的转换。

Newtonsoft.Json.JavaScriptConvert.SerializeObject(object)可以执行json的序列化,也是反序列化的方法。

常见的场景:

A系统提供用户资料(MemberInfo)给子系统B调用,但用户资料中有些内容是不能公开的,如Email地址。

本文由软晨学习网(www.ruanchen.com)发布!转载和采集的话请不要去掉!谢谢。

直接用Newtonsoft.Json.JavaScriptConvert.SerializeObject好像是不行的,它会把object中的所有属性列出。

我的做法是这样的:

定义一个特性类,该特性类只允许用于类的属性上

view plaincopy to clipboardprint?
[AttributeUsage(AttributeTargets.Property)]  
    public class DenyReflectionAttrubite : Attribute  
    {  
        public DenyReflectionAttrubite() { }  
    } 

[AttributeUsage(AttributeTargets.Property)]
    public class DenyReflectionAttrubite : Attribute
    {
        public DenyReflectionAttrubite() { }
    }

MemberInfo类
view plaincopy to clipboardprint?
public class MemberInfo  
    {  
        public int Uid  
        {  
            get;  
            set;  
        }  
        public string UserName  
        {  
            get;  
            set;  
        }  
          
        public string NickName  
        {  
            get;  
            set;  
        }  
        [DenyReflectionAttrubite]  
        public string Password  
        {  
            get;  
            set;  
        }  
       [DenyReflectionAttrubite]  
        public string Email  
        {  
            get;  
            set;  
        }  
//.......................  

public class MemberInfo
    {
        public int Uid
        {
            get;
            set;
        }
        public string UserName
        {
            get;
            set;
        }
       
        public string NickName
        {
            get;
            set;
        }
        [DenyReflectionAttrubite]
        public string Password
        {
            get;
            set;
        }
       [DenyReflectionAttrubite]
        public string Email
        {
            get;
            set;
        }
//.......................
}

至于DenyReflectionAttrubite特性如何使用,下面就会提出。
json生成
view plaincopy to clipboardprint?
public void Builder()  
        {  
            using (jsonWriter = new JsonTextWriter(sw))  
            {  
                  
                jsonWriter.Formatting = Formatting.None;  
                jsonWriter.Indentation = 4;  
                jsonWriter.WriteStartObject();  
                  
                jsonWriter.WritePropertyName("member");  
                jsonWriter.WriteStartArray();  
                jsonWriter.WriteStartObject();  
                Type settingsType = this.member.GetType();  
                foreach (PropertyInfo propertyInformation in settingsType.GetProperties())  
                {  
                    try 
                    {  
                        //在这里对DenyReflectionAttrubite特性的属性跳过处理  
                        if (propertyInformation.GetCustomAttributes(typeof(DenyReflectionAttrubite), false).Length > 0) continue;     
                        object propertyValue = propertyInformation.GetValue(member, null);  
                        string valueAsString = propertyValue.ToString();  
                        if (propertyValue.Equals(null))  
                        {  
                            valueAsString = String.Empty;  
                        }  
                        if (propertyValue.Equals(Int32.MinValue))  
                        {  
                            valueAsString = String.Empty;  
                        }  
                        if (propertyValue.Equals(Single.MinValue))  
                        {  
                            valueAsString = String.Empty;  
                        }  
                        jsonWriter.WritePropertyName(propertyInformation.Name.ToLower());  
                        jsonWriter.WriteValue(valueAsString.Trim());  
                    }  
                    catch { }  
 
                }  
 
                jsonWriter.WriteEndObject();  
                jsonWriter.WriteEnd();  
                jsonWriter.WriteEndObject();  
            }  
        } 

public void Builder()
        {
            using (jsonWriter = new JsonTextWriter(sw))
            {
               
                jsonWriter.Formatting = Formatting.None;
                jsonWriter.Indentation = 4;
                jsonWriter.WriteStartObject();
               
                jsonWriter.WritePropertyName("member");
                jsonWriter.WriteStartArray();
                jsonWriter.WriteStartObject();
                Type settingsType = this.member.GetType();
                foreach (PropertyInfo propertyInformation in settingsType.GetProperties())
                {
                    try
                    {
                        //在这里对DenyReflectionAttrubite特性的属性跳过处理
                        if (propertyInformation.GetCustomAttributes(typeof(DenyReflectionAttrubite), false).Length > 0) continue;  
                        object propertyValue = propertyInformation.GetValue(member, null);
                        string valueAsString = propertyValue.ToString();
                        if (propertyValue.Equals(null))
                        {
                            valueAsString = String.Empty;
                        }
                        if (propertyValue.Equals(Int32.MinValue))
                        {
                            valueAsString = String.Empty;
                        }
                        if (propertyValue.Equals(Single.MinValue))
                        {
                            valueAsString = String.Empty;
                        }
                        jsonWriter.WritePropertyName(propertyInformation.Name.ToLower());
                        jsonWriter.WriteValue(valueAsString.Trim());
                    }
                    catch { }

                }

                jsonWriter.WriteEndObject();
                jsonWriter.WriteEnd();
                jsonWriter.WriteEndObject();
            }
        }

这样就OK了。