序列化使用JSON对象只有部分 [英] serializing only parts of an object with json

查看:107
本文介绍了序列化使用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对象只有部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆