【问题标题】:serializing only parts of an object with json仅使用 json 序列化对象的一部分
【发布时间】:2011-02-14 23:38:07
【问题描述】:

我有一个名为 MyObject 的对象,它有几个属性。 MyList 是我用 linq 查询填充的 MyObject 列表,然后我将 MyList 序列化为 json。我最终得到了这样的东西

    List<MyObject> MyList = new List<MyObject>();

    MyList = TheLinqQuery(TheParam);

    var TheJson = new System.Web.Script.Serialization.JavaScriptSerializer();
    string MyJson = TheJson.Serialize(MyList);

我想要做的是只序列化 MyObject 的一部分。例如,我可能有 Property1、Property2...Propertyn,我希望 MyJson 只包含 Property3、Property5 和 Property8。

我想了一种方法来做到这一点,方法是创建一个只包含我想要的属性的新对象,然后从那里为序列化创建一个新列表。这是最好的方法还是有更好/更快的方法?

谢谢。

【问题讨论】:

    标签: asp.net json


    【解决方案1】:
    // simple dummy object just showing what "MyObject" could potentially be
    public class MyObject
    {
        public String Property1;
        public String Property2;
        public String Property3;
        public String Property4;
        public String Property5;
        public String Property6;
    }
    
    // custom converter that tells the serializer what to do when it sees one of
    // the "MyObject" types. Use our custom method instead of reflection and just
    // dumping properties.
    public class MyObjectConverter : JavaScriptConverter
    {
        public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
        {
            throw new ApplicationException("Serializable only");
        }
    
        public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
        {
            // create a variable we can push the serailized results to
            Dictionary<string, object> result = new Dictionary<string, object>();
    
            // grab the instance of the object
            MyObject myobj = obj as MyObject;
            if (myobj != null)
            {
                // only serailize the properties we want
                result.Add("Property1", myobj.Property1);
                result.Add("Property3", myobj.Property3);
                result.Add("Property5", myobj.Property5);
            }
    
            // return those results
            return result;
        }
    
        public override IEnumerable<Type> SupportedTypes
        {
            // let the serializer know we can accept your "MyObject" type.
            get { return new Type[] { typeof(MyObject) }; }
        }
    }
    

    然后你在哪里序列化:

    // create an instance of the serializer
    JavaScriptSerializer serializer = new JavaScriptSerializer();
    // register our new converter so the serializer knows how to handle our custom object
    serializer.RegisterConverters(new JavaScriptConverter[] { new MyObjectConverter() });
    // and get the results
    String result = serializer.Serialize(MyObjectInstance);
    

    【讨论】:

    • 只是好奇,如果我想对 MyOtherObject 应用相同的技术,我会将第二个覆盖放在哪里?
    • @frenchie:第二次覆盖?制作另一个转换器。
    • 你的意思是添加:public class MyOtherObjectConverter : JavaScriptConverter{.....} 就像上面一样?
    • 覆盖 IEnumerable SupportedTypes 属性有什么作用?
    • @frenchie:您可以使用相同的类并将其他类型添加到列表中,然后在序列化方法中测试它是哪个对象并做出相应的反应。我只是倾向于划分这个功能并将其分解为单独的对象。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-10
    • 1970-01-01
    • 1970-01-01
    • 2021-01-19
    • 2020-05-24
    • 1970-01-01
    相关资源
    最近更新 更多