星空网 > 软件开发 > ASP.net

组件Newtonsoft.Json实现object2json转换

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

最为典型的是生成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();
            }
        }

转载自:http://www.aspnetjia.com




原标题:组件Newtonsoft.Json实现object2json转换

关键词:JS

JS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

W3C Link Checker:https://www.goluckyvip.com/tag/27106.html
W8 W 8BEN :https://www.goluckyvip.com/tag/27107.html
W 8BEN E:https://www.goluckyvip.com/tag/27108.html
W 8ECI:https://www.goluckyvip.com/tag/27109.html
伊朗石油中国:https://www.goluckyvip.com/tag/2711.html
W9税表:https://www.goluckyvip.com/tag/27110.html
洛阳市涧西区有啥好玩的地方 洛阳涧西区附近景点:https://www.vstour.cn/a/408256.html
九月初新疆旅游服装搭配(新疆游玩必备衣服清单):https://www.vstour.cn/a/408257.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流