序列化使用JSON对象只有部分

编程入门 行业动态 更新时间:2024-10-25 18:23:23
本文介绍了序列化使用JSON对象只有部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个对象叫MyObject的有几个属性。 MYLIST是为MyObject,我填充LINQ查询,然后我序列MYLIST成JSON的列表。我结束了这样的事情。

列表<&MyObject的GT; MYLIST =新的List<&MyObject的GT;();    MYLIST = TheLinqQuery(TheParam);    VAR TheJson =新System.Web.Script.Serialization.JavaScriptSerializer();    字符串MyJson = TheJson.Serialize(MYLIST);

我想要做的是序列化myObject的只有部分。举例来说,我可能有Property1,Property2 ...... propertyN中,我想MyJson只包括Property3,Property5和Property8。

我想了个办法用只有我想,从那里创建序列化一个新的列表属性创建一个新的对象来做到这一点。这是最好的方法还是有一个更好的/更快的方式?

感谢。

解决方案

//简单的只是显示什么是MyObject来可能是虚拟对象公共类为MyObject{    公共字符串Property1;    公共字符串Property2;    公共字符串Property3;    公共字符串Property4;    公共字符串Property5;    公共字符串Property6;}//自定义转换器,它告诉序列化时,它看到的人做什么//了MyObject的类型。使用我们的自定义的方法,而不是反思和刚//倾销属性。公共类MyObjectConverter:JavaScriptConverter{    公众覆盖对象的Deserialize(IDictionary的<字符串对象>的字典,输入型的JavaScriptSerializer串行)    {        抛出新ApplicationException的(仅序列化);    }    公共覆盖的IDictionary<字符串对象>序列化(obj对象,序列化的JavaScriptSerializer)    {        //创建一个变量,我们可以推serailized结果        字典<字符串对象>结果=新词典<字符串对象>();        //抓住对象的实例        为MyObject MyObj中的obj =为MyObject的;        如果(MyObj中!= NULL)        {            //只有serailize我们想要的属性            result.Add(Property1,myobj.Property1);            result.Add(Property3,myobj.Property3);            result.Add(Property5,myobj.Property5);        }        //返回这些结果        返回结果;    }    公共覆盖的IEnumerable<类型> SupportedTypes    {        //让串行知道我们可以接受你的MyObject来型。        {返回新类型[] {typeof运算(为MyObject)}; }    }}

然后在任何你正在序列化:

//创建序列化的一个实例串行的JavaScriptSerializer =新的JavaScriptSerializer();//注册一个新的转换器让串行知道如何处理我们的自定义对象serializer.RegisterConverters(新JavaScriptConverter [] {新MyObjectConverter()});//并返回结果字符串结果= serializer.Serialize(MyObjectInstance);

I have an object called MyObject that has several properties. MyList is a list of MyObject that I populate with a linq query and then I serialize MyList into json. I end up with something like this

List<MyObject> MyList = new List<MyObject>(); MyList = TheLinqQuery(TheParam); var TheJson = new System.Web.Script.Serialization.JavaScriptSerializer(); string MyJson = TheJson.Serialize(MyList);

What I want to do is serialize only parts of MyObject. For instance, I might have Property1, Property2...Propertyn and I want MyJson to only include Property3, Property5 and Property8.

I thought of a way to do this by creating a new object with only the properties I want and from there create a new list for the serialization. Is this the best way or is there a better/faster way?

Thanks.

解决方案

// 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) }; } } }

And then where ever you're serializing:

// 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);

更多推荐

序列化使用JSON对象只有部分

本文发布于:2023-11-26 17:30:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634516.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   序列化   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!