2

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.

5
  • 2
    The JSON you listed isn't entirely valid. You can validate it and see the errors with tools like JSON Lint. But, it appears to either have an extra object, {...}, if "Airports" is supposed to be an array or is missing a key to go with the combined object and array. Commented Feb 23, 2014 at 11:29
  • 3
    Where are you getting the JSON from? Can you change it? You can nearly get it to work by creating a class with a List<Airports> Airports property, but even so the value is an object directly containing an array, which I don't think is valid JSON. Commented Feb 23, 2014 at 11:30
  • the JSON is not valid, indeed ! you can check it by yourself putting the json in http://json2csharp.com Commented Feb 23, 2014 at 11:37
  • The json is something that I have created. What I am trying to do is use hard coded json file to create some default data for my application, some data to get things started Commented Feb 23, 2014 at 11:48
  • @JDibble can you change the json format? Commented Feb 23, 2014 at 12:15

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.