使用C#和自定义类反序列化JSON

使用C#和自定义类反序列化JSON,c#,json,deserialization,C#,Json,Deserialization,我有一个应用程序正在接收JSON响应。我一直在尝试将其反序列化,以便可以更轻松地在应用程序中使用数据,但在阅读了其他几篇关于如何使其工作的文章后,我没有取得任何成功。反序列化结束时,列表始终为空。下面是测试JSON数据,下面是我当前的代码 {"vehicles":{"size":10,"next":10,"vehicle":[{"vin":"5GZCZ33Z47S000016","make":"Saturn","model":"Saturn Vue","year":2007,"manufactu

我有一个应用程序正在接收JSON响应。我一直在尝试将其反序列化,以便可以更轻松地在应用程序中使用数据,但在阅读了其他几篇关于如何使其工作的文章后,我没有取得任何成功。反序列化结束时,列表始终为空。下面是测试JSON数据,下面是我当前的代码

{"vehicles":{"size":10,"next":10,"vehicle":[{"vin":"5GZCZ33Z47S000016","make":"Saturn","model":"Saturn Vue","year":2007,"manufacturer":"GM","phone":5002022424,"unitType":"EMBEDDED","primaryDriverId":450984739,"url":"https:\/\/developer.gm.com\/api\/v1\/account\/vehicles\/5GZCZ33Z47S000016","primaryDriverUrl":"https:\/\/developer.gm.com\/api\/v1\/account\/subscribers\/450984739"},{"vin":"1G6DA67VX80000014","make":"Cadillac","model":"STS AWD","year":2008,"manufacturer":"GM","phone":7039277239,"unitType":"EMBEDDED","primaryDriverId":450984752,"url":"https:\/\/developer.gm.com\/api\/v1\/account\/vehicles\/1G6DA67VX80000014","primaryDriverUrl":"https:\/\/developer.gm.com\/api\/v1\/account\/subscribers\/450984752"},{"vin":"1GNFC26069R000015","make":"Cadillac","model":"Suburban (4X2) 4 door","year":2009,"manufacturer":"GM","phone":2815079899,"unitType":"EMBEDDED","primaryDriverId":450984738,"url":"https:\/\/developer.gm.com\/api\/v1\/account\/vehicles\/1GNFC26069R000015","primaryDriverUrl":"https:\/\/developer.gm.com\/api\/v1\/account\/subscribers\/450984738"},{"vin":"1GKUKEEF9AR000010","make":"GMC","model":"Denali","year":2010,"manufacturer":"General Motors","phone":3132200010,"unitType":"EMBEDDED","primaryDriverId":548392002,"url":"https:\/\/developer.gm.com\/api\/v1\/account\/vehicles\/1GKUKEEF9AR000010","primaryDriverUrl":"https:\/\/developer.gm.com\/api\/v1\/account\/subscribers\/548392002"},{"vin":"5TFHW5F15AX000013","make":"Toyota","model":"Tundra 4WD Truck","year":2010,"manufacturer":"Toyota","phone":3372413717,"unitType":"FMV","primaryDriverId":450984766,"url":"https:\/\/developer.gm.com\/api\/v1\/account\/vehicles\/5TFHW5F15AX000013","primaryDriverUrl":"https:\/\/developer.gm.com\/api\/v1\/account\/subscribers\/450984766"},{"vin":"1G1PJ5S95B7000009","make":"Chevrolet","model":"Cruze","year":2011,"manufacturer":"General Motors","phone":8035553074,"unitType":"EMBEDDED","primaryDriverId":548392002,"url":"https:\/\/developer.gm.com\/api\/v1\/account\/vehicles\/1G1PJ5S95B7000009","primaryDriverUrl":"https:\/\/developer.gm.com\/api\/v1\/account\/subscribers\/548392002"},{"vin":"2CNALPEC3B6000001","make":"Chevrolet","model":"Equinox","year":2011,"manufacturer":"General Motors","phone":3135450001,"unitType":"EMBEDDED","primaryDriverId":450984732,"url":"https:\/\/developer.gm.com\/api\/v1\/account\/vehicles\/2CNALPEC3B6000001","primaryDriverUrl":"https:\/\/developer.gm.com\/api\/v1\/account\/subscribers\/450984732"},{"vin":"1GCRCSE09BZ000005","make":"Chevrolet","model":"Silverado Crew Cab","year":2011,"manufacturer":"General Motors","phone":8035553074,"unitType":"EMBEDDED","primaryDriverId":450984732,"url":"https:\/\/developer.gm.com\/api\/v1\/account\/vehicles\/1GCRCSE09BZ000005","primaryDriverUrl":"https:\/\/developer.gm.com\/api\/v1\/account\/subscribers\/450984732"},{"vin":"1G1RD6E47BU000008","make":"Chevrolet","model":"Volt","year":2011,"manufacturer":"General Motors","phone":3139030008,"unitType":"EMBEDDED","primaryDriverId":548392002,"url":"https:\/\/developer.gm.com\/api\/v1\/account\/vehicles\/1G1RD6E47BU000008","primaryDriverUrl":"https:\/\/developer.gm.com\/api\/v1\/account\/subscribers\/548392002"},{"vin":"1G6DH5E53C0000003","make":"Cadillac","model":"CTS Coupe","year":2012,"manufacturer":"General Motors","phone":3136440003,"unitType":"EMBEDDED","primaryDriverId":450438292,"url":"https:\/\/developer.gm.com\/api\/v1\/account\/vehicles\/1G6DH5E53C0000003","primaryDriverUrl":"https:\/\/developer.gm.com\/api\/v1\/account\/subscribers\/450438292"}]}}
这是主窗体中的函数,用于处理将信息传递到对象的过程

void Deserialize(IRestResponse response)
{
        string workingString = response.Content;

        var list = new JavaScriptSerializer().Deserialize<List<GMVehicles>>(workingString);
}

我做错了什么?

您的
车辆
等级很好。现在,您需要在
GMVehicles
类周围安装一个包装器:

public class Root
{
    public GMVehicles Vehicles { get; set; }
}
此外,您的
gmvehicle
错误,它应该包含一个集合(因为JSON中的
vehicle
属性是一个列表,而不是一个对象):

好的,现在反序列化根:

void Deserialize(IRestResponse response)
{
    string workingString = response.Content;
    var root = new JavaScriptSerializer().Deserialize<Root>(workingString);
    Vehicle[] list = root.Vehicles.Vehicle;
    // ... do something with the list of vehicles here
}
void反序列化(IRestResponse响应)
{
字符串工作字符串=response.Content;
var root=new JavaScriptSerializer()。反序列化(workingString);
车辆[]列表=root.Vehicles.Vehicle;
//…对这里的车辆列表做些什么
}
public class GMVehicles
{
    public Vehicle[] Vehicle { get; set; }
}
void Deserialize(IRestResponse response)
{
    string workingString = response.Content;
    var root = new JavaScriptSerializer().Deserialize<Root>(workingString);
    Vehicle[] list = root.Vehicles.Vehicle;
    // ... do something with the list of vehicles here
}