程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> 關於ASP.NET >> 組件Newtonsoft.Json實現object2json轉換

組件Newtonsoft.Json實現object2json轉換

編輯:關於ASP.NET

很多情況下,我們需要把數據類型做一些轉換,供其它外部的子系統調用。

最為典型的是生成json格式供javascript作調用。

現成的組件Newtonsoft.Json可以實現object2json之間的轉換。

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

常見的場景:

A系統提供用戶資料(MemberInfo)給子系統B調用,但用戶資料中有些內容是不能公開的,如Email地址。

本文由網頁教學網(www.webjx.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了。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved