2013-06-23 2 views
0

У меня есть приложение, получающее ответ JSON. Я пытался его десериализовать, поэтому я могу более легко использовать данные в приложении, но, пройдя несколько других статей о том, как заставить это работать, я не имел никакого успеха. Список всегда пуст в конце десериализации. Ниже приведены тестовые данные JSON, а ниже - мой текущий код.Deserializing JSON с использованием C# и пользовательских классов

{"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); 
} 

Эти классы, что deserialzer следует положить данные

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; } 
} 

Что я делаю неправильно?

ответ

1

Ваш Vehicle класс в порядке. Теперь вам нужна обертка вокруг GMVehicles класса:

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

также ваш GMVehicles неправильно, он должен содержать коллекцию (потому что vehicle свойства в вашем JSON список, а не объект):

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 
} 
+0

Спасибо так много! Это сработало отлично! –

Смежные вопросы