【问题标题】:JSON.NET with Custom Serializer to a custom object带有自定义序列化器的 JSON.NET 到自定义对象
【发布时间】:2013-01-30 15:05:52
【问题描述】:

所以我试图将我的复杂对象 .NET 转换为 JSON 中完全不同的对象。基本上,我有一个对象数组,可以是从我的基类派生的任何类型,我想将该对象数组转换为我自己的 JSON 对象。我认为我不能只做一个简单的 JsonConvert.Serialize() 方法调用,因为我的 JSON 对象的结构会有所不同。

所以这是我的 .NET 类的模型:

public abstract class A
{
   public string Id { get; set; }
   public string Title { get; set; }
   public bool Enabled { get; set; }
   public List<X> MyCollection { get; set; }
}

public class B : A
{
   public string Foo { get; set; }
}

public class C : A
{
   public int Bar { get; set; }
}

public abstract class X
{
   public string Id { get; set; }
   public string Title { get; set; }
   public bool Enabled { get; set; }
}

public class Y : X
{
   public string Hello { get; set; }
}

public class Z : X
{
   public string World { get; set; }
}

显然,这是我真实班级结构的简单视图,但希望有关如何转换它的一些指导将使我能够转换我的真实班级。基本上,我的类 (A,B,C) 将包含一个类列表 (X,Y,C)。

假设我有一个上面列出的这些对象的数组/集合:

List<A> myObjects = new List<A>();
A myVar = new B();
myVar.Title = "Number 1";
myVar.MyCollection.Add(new Y());
myVar.MyCollection.Add(new Y());
myVar.MyCollection.Add(new Z());
myObjects.Add(myVar);

myVar = new B();
myVar.Title = "Number 2";
myVar.MyCollection.Add(new Z());
myObjects.Add(myVar);

myVar = new C();
myVar.Title = "Number 3";
myVar.MyCollection.Add(new Y());
myVar.MyCollection.Add(new Y());
myObjects.Add(myVar);

我想将我的对象“myObjects”序列化成这样的 JSON 结构:

[
   {
      name: "Number 1", //This is the value of A.Title
      type: "B", //This is a NEW property I want to add to the JSON
      enabled: true, //This is the value of A.Enabled
      foo: "SomeValue", //This is the value of B.Foo
      myCollection: [
         { name: "Y1", type: "Y", enabled: true, hello: "SomeValue" }
         { name: "Y2", type: "Y", enabled: true, hello: "SomeOtherValue" }
         { name: "Z1", type: "Z", enabled: true, world: "SomeValue" }
      ]
   },
   {
      name: "Number 2",
      type: "B",
      enabled: true,
      foo: "SomeValue",
      myCollection: [
         { name: "Z2", type: "Z", enabled: true, world: "SomeValue" }
      ]
   },
   {
      name: "Number 3",
      type: "C",
      enabled: true,
      bar: "SomeValue",
      myCollection: [
         { name: "Y3", type: "Y", enabled: true, hello: "SomeValue" }
         { name: "Y4", type: "Y", enabled: true, hello: "SomeOtherValue" }
      ]
   }
]

基本上,我想添加我自己的属性并在 JSON 中构造它,这与我的 .NET 对象所反映的有所不同。还有一些其他属性我也想添加到我的对象中,但它们没有在这里列出(可能这个帖子会更大)。我基本上只需要一种方法来获取我的对象并以我自己的自定义方式序列化它们。我需要确保我序列化派生类型,以便我可以携带其中一些属性。任何人都可以帮助指导我解决这个问题的正确方向吗?

【问题讨论】:

  • 在 ASP .NET MVC 中,您可以只使用 return Json(myObjects);,但您需要根据您想要的预期结果对 myObjects 进行建模。

标签: c# .net json .net-4.0 json.net


【解决方案1】:

您可以使用 Json.Linq 类通过在反序列化主类后读取类型来动态反序列化它们。

var objects = JArray.Parse(jsonString)
List<A> objectList = new List<A>();
foreach (var obj in objects) {
    A newObj = null;
    switch ((string)obj["type"]) {
        case "B":
            newObj = obj.ToObject<B>();
            break;
        case "C":
            newObj = obj.ToObject<C>();
            break;
    }
    newObj.MyCollection.Clear();
    foreach (var x in obj["myCollection"]) {
        var newX = null;
        switch ((string)x["type"]) {
            case "Y":
                newX = x.ToObject<Y>();
                break;
            case "Z":
                newX = x.ToObject<Z>();
                break;
        }
        newObj.MyCollection.Add(newX);
    }
    objectList.Add(newObj);
}

据我所知,这将处理您发布的 Json,如果有任何错误,对不起,我完全是在平板电脑上凭记忆完成的:P

【讨论】:

  • 我最终选择了另一种解决方案。我认为如果我需要走这条路,这样的事情会起作用。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-30
  • 2011-07-21
  • 1970-01-01
  • 2021-10-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多