C# 带有自定义序列化程序的JSON.NET到自定义对象

C# 带有自定义序列化程序的JSON.NET到自定义对象,c#,.net,json,.net-4.0,json.net,C#,.net,Json,.net 4.0,Json.net,因此,我试图将我的复杂对象.NET转换成JSON中完全不同的对象。基本上,我有一个对象数组,可以是从我的基类派生的任何类型,我想把这个对象数组转换成我自己的JSON对象。我不认为我可以只调用一个简单的JsonConvert.Serialize()方法,因为我的JSON对象的结构将不同 下面是我的.NET类的模型: public abstract class A { public string Id { get; set; } public string Title { get; se

因此,我试图将我的复杂对象.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; }
}

基本上,我想添加我自己的属性,并在JSON中对其进行一些不同于.NET对象的构造。还有一些其他属性我也想添加到我的对象中,但它们没有在这里列出(这篇文章可能会更大)。我基本上只需要一种方法来获取我的对象并以我自己的自定义方式序列化它们。我需要确保我序列化了派生类型,这样我就可以携带其中的一些属性了。有谁能帮助我找到解决这个问题的正确方向吗?

在反序列化主类后,您可以使用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);
}
var objects=JArray.Parse(jsonString)
List objectList=新列表();
foreach(对象中的var obj){
A newObj=null;
开关((字符串)obj[“类型”]){
案例“B”:
newObj=obj.ToObject();
打破
案例“C”:
newObj=obj.ToObject();
打破
}
newObj.MyCollection.Clear();
foreach(obj[“myCollection”]中的变量x){
var newX=null;
开关((字符串)x[“类型”]){
案例“Y”:
newX=x.ToObject();
打破
案例“Z”:
newX=x.ToObject();
打破
}
newObj.MyCollection.Add(newX);
}
objectList.Add(newObj);
}

据我所知,这将处理您发布的Json,如果有任何错误,很抱歉我完全是从平板电脑上的内存中完成的:p

在ASP.NETMVC中,您可以
返回Json(myObjects)[
   {
      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" }
      ]
   }
]
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);
}