ASP.NET中Json序列化指定对象的部分属性值

一、实现代码


    using System;
    using System.Collections.Generic;
    using System.Web.Script.Serialization;



    public class Person
    {
        public string Name { get; set; }

        public int Age { get; set; }

        public double Meney { get; set; }

        public double Tex { get; set; }

        public DateTime Berthday { get; set; }

    }

    /** <summary>
    ///简单实体 可变属性序列化器
    /// </summary>
    public class PropertyVariableJsonSerializer
    {
        readonly System.Web.Script.Serialization.JavaScriptSerializer _serializer = new JavaScriptSerializer();

        /** <summary>
        /// json 序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <param name="propertys"></param>
        /// <returns></returns>
        public string Serialize<T>(T obj, List<string> propertys)
        {
            _serializer.RegisterConverters(new[] { new PropertyVariableConveter(typeof(T), propertys) });

            return _serializer.Serialize(obj);

        }
    }


    public class PropertyVariableConveter : JavaScriptConverter
    {
        private readonly List<Type> _supportedTypes = new List<Type>();

        public PropertyVariableConveter(Type supportedType, List<string> propertys)
        {
            _supportedTypes.Add(supportedType);
            Propertys = propertys;

        }


        private List<string> Propertys { get; set; }


        public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
        {

            throw new Exception("  这个暂时不支持 , 谢谢 ");

        }

        public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
        {
            var dic = new Dictionary<string, object>();

            var t = obj.GetType();
            var properties = t.GetProperties();

            foreach (var ite in properties)
            {
                string key = ite.Name;
                var v = t.GetProperty(key).GetValue(obj, null);

                if (Propertys == null || Propertys.Count <= 0)
                {
                    dic.Add(key, v);
                    continue;
                }

                if (Propertys.Contains(key))
                {
                    dic.Add(key, v);
                }
            }

            return dic;

        }

        public override IEnumerable<Type> SupportedTypes
        {
            get { return _supportedTypes; }
        }
    }


    


二、调用


        public static void aaa()
        {
            var p = new Person { Age = 20, Name = "www.studyofnet.com", Meney = 3, Tex = 1};

            var s = new PropertyVariableJsonSerializer();

            string result = s.Serialize<Person>(p, new List<string>() { "Name", "Age" });
        }




文章转载自: ASP.NET中Json序列化指定对象的部分属性值   http://www.studyofnet.com/news/1234.html





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值