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

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

因此,我试图将我的复杂对象 .NET 转换为完全不同的 JSON 对象。基本上,我有一个对象数组,可以是派生 self 的基类的任何类型,我想将该对象数组转换为我自己的 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; }
}

显然,这是我的真实类结构的简单 View ,但希望一些关于如何转换它的指导将使我能够转换我的真实类。基本上,我的类 (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 对象所反射(reflect)的略有不同。还有一些我想添加到我的对象的其他属性,但它们没有在这里列出(这篇文章可能会更大)。我基本上只需要一种方法来获取我的对象并以我自己的自定义方式序列化它们。我需要确保对派生类型进行序列化,以便我可以携带其中的一些属性。任何人都可以帮助指导我朝着正确的方向解决这个问题吗?

最佳答案

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

关于c# - 带有自定义序列化程序的 JSON.NET 到自定义对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14607099/

相关文章:

.net - 为什么ContinueWith传Task作为参数

.net - 在 Silverlight 中禁用右键单击

java - 如何从自定义 Gson JsonSerializer 调用另一个序列化程序?

c# - 存储通用对象的列表/字典

c# - Where() 的意外结果

c# - 无法在 Chromium 嵌入式框架中播放视频 (mp4)

从 Visual Studio 启动时,C# ReleaseMutex() 不会在控制台应用程序的多个实例中释放互斥量

c# - 不推荐使用的成员变量

arrays - 在 Elasticsearch 中搜索数组时获取对象命中

java - gson 无法使用 GsonBuilder.setDateFormat ("yyyy-MM-dd' T'HH :mm:ss. SSSZ"进行解析"