使用嵌套列表时与反序列化对象组成 [英] deserializing object with composition when using nested lists

查看:117
本文介绍了使用嵌套列表时与反序列化对象组成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含其他对象名单的对象,看起来像这样:

I have an object that contains lists of other objects and that looks like this:

public class MyObject {

 public int Prop1 { get; set; }
 public List<Object1> TheListOfObject1 { get; set; }
 public List<Object2> TheListOfObject2 { get; set; }
 public string MyObjectInJson { get; set;}

 public void MyObjectToJson()
 {
  JavascriptSerializer TheSerializer = new JavascriptSerializer();
  TheSerializer.RegisterConverters(new JavaScriptConverter[] { new Object1ToJson() });
  TheSerializer.RegisterConverters(new JavaScriptConverter[] { new Object2ToJson() });
  MyObjectInJson = TheSerializer.Serialize(this);
}

现在我有另一个类,它是越来越MyObject来的JSON字符串,我需要deserializse字符串并创建一个从字符串MyObject的。

Now I have another class that's getting a json string of MyObject and I need to deserializse the string and create a MyObject from the string.

我知道有JSON.net和其他图书馆提供,但我想用.NET的的JavaScriptSerializer。

I know there's JSON.net and other library available but I want to use .net's JavascriptSerializer.

假设转换器,我也处理deserializtion。难道我只是做这样的事情:

Suppose that the converters I have also handle the deserializtion. Do I simply do something like this:

1)添加FromJson方法为myObject

1) add FromJson method to MyObject

  public this MyObjectFromJson (string MyObjectInJson)
    {
      JavascriptSerializer TheSerializer = new JavascriptSerializer();
      TheSerializer.RegisterConverters(new JavaScriptConverter[] { new Object1ToJson() });
      TheSerializer.RegisterConverters(new JavaScriptConverter[] { new Object2ToJson() });
      this = TheSerializer.DeSerialize(MyObjectInJson);
    }

2),在调用类这样写:

2) and in the calling class write this:

MyObject TheObject = new MyObject();
TheObject = TheObject.MyObjectFromJson(TheIncomingString);

我不知道如何着手。将这种方法工作的?

I'm not sure how to proceed. Will this kind of approach work?

感谢。

推荐答案

除非你Object1和Object2的名单是极其复杂的,你可能不需要转换器。

Unless your lists of Object1 and Object2 are incredibly complex, you probably don't need the Converters.

下面是我扔在一起根据您的code的快速样本。需要注意的是解串器是静态的,因为你会想它来创建一个新的对象,则不能设置这个,因为它是只读的。

Here is a quick sample that I threw together based on your code. Note that the deserializer is static since you will want it to create a new object and you can't set this because it is readonly.

public class MyObject
{
    public int Prop1 { get; set; }
    public List<Object1> TheListOfObject1 { get; set; }
    public List<Object2> TheListOfObject2 { get; set; }

    public MyObject()
    {
        TheListOfObject1 = new List<Object1>();
        TheListOfObject2 = new List<Object2>();
    }

    public string ToJson()
    {
        JavaScriptSerializer TheSerializer = new JavaScriptSerializer();
        return TheSerializer.Serialize(this);
    }
    public static MyObject FromJson(string sValue)
    {
        JavaScriptSerializer TheSerializer = new JavaScriptSerializer();
        return TheSerializer.Deserialize<MyObject>(sValue);
    }
}

这然后被称为

        MyObject oObject = new MyObject();
        oObject.TheListOfObject1.Add(new Object1(1));
        oObject.TheListOfObject2.Add(new Object2(2));
        oObject.Prop1 = 3;

        string sJSON = oObject.ToJson();

        System.Diagnostics.Debug.WriteLine(sJSON);

        oObject = MyObject.FromJson(sJSON);

        System.Diagnostics.Debug.WriteLine(oObject.Prop1);

这篇关于使用嵌套列表时与反序列化对象组成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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