Newtonsoft JSON反序列化具有不同对象类型的数组

时间:2014-05-22 16:15:24

标签: c# json.net

我正在尝试以最有效的方式反序列化json数组中的不同对象类型,但我无法真正看到如何以任何直接的方式完成此操作。我正在使用Newtonsoft.Json库进行解析。

我已经设置了一个简单的例子来说明我想要解决的问题,我希望绑定的类和我正在使用的JSON格式定义如下:

class Car
{
    public int Wheels { get; set; }
    public String Manufacturer { get; set; }
    public String Colour { get; set; }
    public double EngineSize { get; set; }
    public String Year { get; set; }
}

class Aeroplane
{
    public String Model { get; set; }
    public String Airline { get; set; }
    public int JourneyCount { get; set; }
}

class Ship
{
    public String Name { get; set; }
    public String Company { get; set; }
    public int Capacity { get; set; }
    public String YearBuilt { get; set; }
}

这是我试图绑定到这些类的JSON。它被设置为一个数组,每个位置有三种不同的嵌套对象类型。基本上,数组中的每个元素/索引都包含三个对象,即汽车,飞机和船舶。

{[
  {
    "car": {
      "wheels": 4,
      "manufacturer": BMW,
      "colour": "Black",
      "engineSize": "1.6"
    },
    "aeroplane": null,
    "ship": null
  },
  {
    "car": {
      "wheels": 4,
      "manufacturer": BMW,
      "colour": "Black",
      "engineSize": "1.6"
    },
    "aeroplane": {
        "model": "Dreamliner",
        "airline": "Emirates",
        "journeyCount": 4798
    },
    "ship": {
        "name":"Queen Mary",
        "company":"Cunard",
        "capacity": 890,
        "yearBuilt": 2000
    }
  },
  {
    "car": {
      "wheels": 4,
      "manufacturer": BMW,
      "colour": "Black",
      "engineSize": "1.6"
    },
    "aeroplane": {
        "model": "Boeing 777",
        "airline": "BA",
        "journeyCount": 6158
    },
    "ship": {
        "name":"HMS Diamond",
        "company":"Royal Navy",
        "capacity": 500,
        "yearBuilt": 2010
    }
  }
]}

1 个答案:

答案 0 :(得分:4)

首先你的json无效。为了使它有效,你需要摆脱顶部花括号。还有一些缺失的引号。您可以使用JSON Lint对其进行验证。它应该是这样的:

[
    {
        "car": {
            "wheels": 4,
            "manufacturer": "BMW",
            "colour": "Black",
            "engineSize": "1.6"
        },
        "aeroplane": null,
        "ship": null
    },
    {
        "car": {
            "wheels": 4,
            "manufacturer": "BMW",
            "colour": "Black",
            "engineSize": "1.6"
        },
        "aeroplane": {
            "model": "Dreamliner",
            "airline": "Emirates",
            "journeyCount": 4798
        },
        "ship": {
            "name": "Queen Mary",
            "company": "Cunard",
            "capacity": 890,
            "yearBuilt": 2000
        }
    },
    {
        "car": {
            "wheels": 4,
            "manufacturer": "BMW",
            "colour": "Black",
            "engineSize": "1.6"
        },
        "aeroplane": {
            "model": "Boeing 777",
            "airline": "BA",
            "journeyCount": 6158
        },
        "ship": {
            "name": "HMS Diamond",
            "company": "Royal Navy",
            "capacity": 500,
            "yearBuilt": 2010
        }
    }
]

你还需要一个课程来包装汽车,飞机和轮船:

public class VehicleWrapper
{
    public Car Car { get; set; }

    public Aeroplane Aeroplane { get; set; }

    public Ship ship { get; set; }
}

现在您可以使用以下行反序列化它:

List<VehicleWrapper> vehicles = JsonConvert.DeserializeObject<List<VehicleWrapper>>(jsonString);