简体   繁体   English

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

[英]Deserializing JSON using C# and custom classes

I've got an application that is receiving a JSON response. 我有一个正在接收JSON响应的应用程序。 I've been trying to get it to deserialize, so I can more easily use the data in the application, but after going through several other articles on how to get this to work, I haven't had any success. 我一直在尝试对其进行反序列化,因此我可以更轻松地在应用程序中使用数据,但是在阅读了其他几篇有关如何使其工作的文章之后,我没有取得任何成功。 The List is always empty at the end of the deserialize. 反序列化结束时,列表始终为空。 The test JSON data is below, and below that is my current code. 下面是测试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"}]}}

This is the function from the Main Form that is handling passing the info into objects. 这是Main Form中的函数,该函数正在处理将信息传递给对象的过程。

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

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

These are the classes that the deserialzer should be putting the data 这些是解串器应放置数据的类

public class GMVehicles
{
    public Vehicle vehicle { get; set; }
}

public class Vehicle
{
    public string vin { get; set; }
    public string make { get; set; }
    public string model { get; set; }
    public string year { get; set; }
    public string manufacturer { get; set; }
    public string phone { get; set; }
    public string unitType { get; set; }
    public string primaryDriverId { get; set; }
    public string url { get; set; }
    public string primaryDriverUrl { get; set; }
}

What am I doing wrong? 我究竟做错了什么?

Your Vehicle class is fine. 您的Vehicle等级很好。 Now you need a wrapper around your GMVehicles class: 现在,您需要围绕GMVehicles类进行包装:

public class Root
{
    public GMVehicles Vehicles { get; set; }
}

also your GMVehicles is wrong, it should contain a collection (because the vehicle property in your JSON is a list, not an object): 同样,您的GMVehicles是错误的,它应该包含一个集合(因为JSON中的vehicle属性是一个列表,而不是一个对象):

public class GMVehicles
{
    public Vehicle[] Vehicle { get; set; }
}

Alright, now you deserialize the root: 好了,现在您反序列化根目录:

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM