JDibble
JDibble

Reputation: 744

Exception thrown when deserializing json with JSON.NET

I have the following class

public class Airport
{
    [MaxLength(75)]
    public string Name { get; set; }

    public bool NotInUse { get; set; }

    [MaxLength(50)]
    public string City { get; set; }

    [MaxLength(50)]
    public string Country { get; set; }

    [MaxLength(2)]
    public string Iso { get; set; }

    [MaxLength(3)]
    public string Iata { get; set; }

    [MaxLength(4)]
    public string Icao { get; set; }
}

I have the following json file - Not all properties are within the json

    {
  "Airports":{
  [
    {
       "Name": "Belfast International",
       "City": "Belfast",
       "Country": "United Kingdom",
       "Iso": "GB",
       "Iata": "BFS"
     },
     {
       "Name": "City of Derry",
       "City": "Derry",
       "Country": "United Kingdom",
       "Iso": "GB",
       "Iata": "LDY"

     }
    ]
  }
}

I am trying to deserialise the json with this method

public IList<Airport> ReadAirportsFromJson()
{
    if (File.Exists(AirportJsonFilename))
    {
        string fileContents = File.ReadAllText(AirportJsonFilename);
        var airports = JsonConvert.DeserializeObject<List<Airport>>(fileContents);
        return airports;
    }
    return null;
}

I get the following exception

Screenshot

I am unsure how to progress this and resolve the issue.

Upvotes: 2

Views: 4215

Answers (2)

ekad
ekad

Reputation: 14614

The json is not valid, I'd suggest changing it to something like this

{
  "Airports":
  [
    {
       "Name": "Belfast International",
       "City": "Belfast",
       "Country": "United Kingdom",
       "Iso": "GB",
       "Iata": "BFS"
     },
     {
       "Name": "City of Derry",
       "City": "Derry",
       "Country": "United Kingdom",
       "Iso": "GB",
       "Iata": "LDY"

     }
    ]
}

and create a wrapper class

public class AirportsWrapper
{
    public List<Airport> Airports { get; set; }
}

You can deserialize the json into AirportsWrapper and return the Airports property

public IList<Airport> ReadAirportsFromJson()
{
    if (File.Exists(AirportJsonFilename))
    {
        string fileContents = File.ReadAllText(AirportJsonFilename);
        var airportsWrapper = JsonConvert.DeserializeObject<AirportsWrapper>(fileContents);
        if (airportsWrapper != null)
        {
            return airportsWrapper.Airports;
        }
    }
    return null;
}

Demo: https://dotnetfiddle.net/NQ8JfQ

Upvotes: 2

Arve Systad
Arve Systad

Reputation: 5479

When passing it in like you do there, you're trying to deserialize the entire file into a list of airports. If the entire file is something else that also happens to contain a list of airports, this won't work. You either have to get just the parts of the JSON which is actually the list of airports, or you have to make a class/classes to represent the entire JSON file and deserialize into that. From there on, you can just go to yourObject.Airports.

Upvotes: 0

Related Questions