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

組件Newtonsoft.Json實現object2json轉換

編輯:關於.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