Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I get such JSON and want to deserialize it into my C# object.

JSON (partially):

{
    "Quotes": [
        {
            "QuoteId": 1,
            "MinPrice": 119,
            "Direct": true,
            "OutboundLeg": {
                "CarrierIds": [
                    851
                ],
                "OriginId": 81727,
                "DestinationId": 60987,
                "DepartureDate": "2019-04-01T00:00:00"
            },
            "QuoteDateTime": "2019-03-10T19:27:00"
        }
    ],
    "Places": [
        {
            "PlaceId": 60987,
            "IataCode": "JFK",
            "Name": "New York John F. Kennedy",
            "Type": "Station",
            "SkyscannerCode": "JFK",
            "CityName": "New York",
            "CityId": "NYCA",
            "CountryName": "United States"
        },
        {
            "PlaceId": 81727,
            "IataCode": "SFO",
            "Name": "San Francisco International",
            "Type": "Station",
            "SkyscannerCode": "SFO",
            "CityName": "San Francisco",
            "CityId": "SFOA",
            "CountryName": "United States"
        }
    ]   
}


My classes:
public class MyQuotes

    {
        public List<Quote> Quotes { get; set; }
        public List<Place> Places { get; set; }
    }

    public class Quote
    {
        public int QuoteId { get; set; }
        public List<MyOutboundLeg> OutboundLeg { get; set; }
    }

    public class MyOutboundLeg
    {
        public List<int> CarrierIds { get; set; }
        public int OriginId { get; set; }
    }

    public class Place
    { 
        public string PlaceId { get; set; }
        public string CountryName { get; set; }
    }
    
    }


What I have tried:

var body = ApiTestContext.Response.Content.ReadAsStringAsync().Result;
            MyQuotes myQuotes = JsonConvert.DeserializeObject<MyQuotes>(body);
Posted
Updated 11-Mar-19 3:46am
v2

Don't all of the fields in the json have to be present in the class, AND don't they all have to be named the same in the JSON and the class? How else is it going to be able to serialize?

You're missing fields in your class definition, and one of the fields isn't named the same as what's in the json data (this is where the exception is happening - JSON has "Name", and your class has "PlaceName").
 
Share this answer
 
v4
Comments
Member 14178219 11-Mar-19 9:45am    
Thank you, I have renamed the field "PlaceName", but as I know not all of the fields from JSON have to be present in the class, only fields which I want to use later. And I still get the same error
That's because your JSON doesn't match your class definitions.
Try an online generator (here's the one I use: json2csharp - generate c# classes from json[^] ) and you get this:
public class OutboundLeg
{
    public List<int> CarrierIds { get; set; }
    public int OriginId { get; set; }
    public int DestinationId { get; set; }
    public DateTime DepartureDate { get; set; }
}

public class Quote
{
    public int QuoteId { get; set; }
    public int MinPrice { get; set; }
    public bool Direct { get; set; }
    public OutboundLeg OutboundLeg { get; set; }
    public DateTime QuoteDateTime { get; set; }
}

public class Place
{
    public int PlaceId { get; set; }
    public string IataCode { get; set; }
    public string Name { get; set; }
    public string Type { get; set; }
    public string SkyscannerCode { get; set; }
    public string CityName { get; set; }
    public string CityId { get; set; }
    public string CountryName { get; set; }
}

public class RootObject
{
    public List<Quote> Quotes { get; set; }
    public List<Place> Places { get; set; }
}
 
Share this answer
 
Comments
Member 14178219 11-Mar-19 9:57am    
thank you, I will change the code, can I ask is it mandatory use all fields? I mean before I used just some fields which I will use and for other tests it worked
OriginalGriff 11-Mar-19 10:55am    
You don't have to have all the fields in the class in teh JSON, but you do have to have all the fields in the JSON in the class.
It's a good idea to have an exact match, so you don't get future compatibility problems.
Member 14178219 11-Mar-19 10:58am    
thanks
OriginalGriff 11-Mar-19 11:00am    
You're welcome!
Your Outbound Leg class is a property in your JSON but a List in your Quote class:

public class Quote
    {
        public int QuoteId { get; set; }
        public List<MyOutboundLeg> OutboundLeg { get; set; }
    }


Should be:

public class Quote
    {		
        public int QuoteId { get; set; }
        public MyOutboundLeg OutboundLeg { get; set; }
    }
 
Share this answer
 
Comments
Member 14178219 11-Mar-19 9:58am    
thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900