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

本文关键字:对象 类型 JSON 反序列化 数组 Newtonsoft | 更新日期: 2023-09-27 18:04:43

我试图以最有效的方式在json数组中反序列化不同的对象类型,但我真的看不出这是如何以任何直接的方式完成的。我用的是牛顿软件。解析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
    }
  }
]}

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

首先你的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);