簡體   English   中英

帶自定義序列化程序的JSON.NET到自定義對象

[英]JSON.NET with Custom Serializer to a custom object

所以我試圖將我的復雜對象.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對象反映的略有不同。 還有一些其他屬性我想添加到我的對象,但它們沒有列在這里(可能會發布更大的EVEN)。 我基本上只需要一種方法來獲取我的對象並以我自己的自定義方式序列化它們。 我需要確保我序列化派生類型,以便我可以攜帶其中一些屬性。 任何人都可以幫助指導我如何解決這個問題的正確方向?

您可以使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM