Search code examples
c#json.netjson-deserialization

Unable to Parse JSON using NewtonSoft JSONConvert


I am trying to deserialize the below JSON.

{
  "cinemas": [
    {
      "id": "44973",
      "slug": "amc-star-fairlane-21-dearborn",
      "name": "AMC Star Fairlane 21",
      "chain_id": "26",
      "telephone": "(313) 593-3331",
      "website": "https://www.amctheatres.com/movie-theatres/detroit/amc-star-fairlane-21",
      "location": {
        "lat": 42.3177314,
        "lon": -83.2243335,
        "address": {
          "display_text": "18900 Michigan Ave., Dearborn, MI 48126, United States",
          "street": "Michigan Ave",
          "house": "18900",
          "zipcode": "48126",
          "city": "Dearborn",
          "state": "Michigan",
          "state_abbr": "MI",
          "country": "United States",
          "country_code": "US"
        }
      },
      "booking_type": "external"
    },
    {
      "id": "44978",
      "slug": "allen-park-digital-cinema",
      "name": "Allen Park Digital Cinema",
      "chain_id": null,
      "telephone": "(313) 381-1125",
      "website": "http://www.allenparkcinemas.com/",
      "location": {
        "lat": 42.2578377,
        "lon": -83.2083368,
        "address": {
          "display_text": "6601 Allen Rd, Allen Park, MI 48101, United States",
          "street": "6601 Allen Road",
          "house": "6601",
          "zipcode": "48101",
          "city": "Allen Park",
          "state": "Michigan",
          "state_abbr": "MI",
          "country": "United States",
          "country_code": "US"
        }
      },
      "booking_type": null
    }
  ]
}

I am using Newtonsoft.JSON in for .NET.

var data = JsonConvert.DeserializeObject<CinemasViewModel>(response);

My class mappings are as below.

namespace HttpClientTest
{
    public class CinemasViewModel
    {
        [JsonProperty("cinemas")]
        public List<Cinema> Cinemas { get; set; }
    }

    public class Cinema
    {
        [JsonProperty("id")]
        public string Id { get; set; }

        [JsonProperty("slug")]
        public string Slug { get; set; }

        [JsonProperty("name")]
        public string Name { get; set; }

        [JsonProperty("chain_id")]
        public string ChainId { get; set; }

        [JsonProperty("telephone")]
        public string Telephone { get; set; }

        [JsonProperty("email")]
        public string Email { get; set; }

        [JsonProperty("website")]
        public string Website { get; set; }

        [JsonProperty("booking_type")]
        public string BookingType { get; set; }


        [JsonProperty("location")]
        public Location Location { get; set; }

    }

    public class Location
    {
        [JsonProperty("lat")]
        public double Latitude { get; set; }

        [JsonProperty("lon")]
        public double Longitude { get; set; }

        [JsonProperty("address")]
        public string Address { get; set; }
    }

    public class Address
    {
        [JsonProperty("display_text")]
        public string DisplayText { get; set; }

        [JsonProperty("street")]
        public string Street { get; set; }

        [JsonProperty("house")]
        public string House { get; set; }

        [JsonProperty("zipcode")]
        public string ZipCode { get; set; }

        [JsonProperty("city")]
        public string City { get; set; }

        [JsonProperty("state")]
        public string State { get; set; }

        [JsonProperty("state_abbr")]
        public string StateAbbreviation { get; set; }

        [JsonProperty("country")]
        public string Country { get; set; }

        [JsonProperty("country_code")]
        public string CountryCode { get; set; }
    }
}

I am getting a 'Newtonsoft.Json.JsonReaderException' with Message

{"Unexpected character encountered while parsing value: {. Path 'cinemas[0].location.address', line 1, position 282."}

Please help me solve this issue. Any help would be greatly appreciated.


Solution

  • Your problem is that, in your Location class, you have the following property:

        [JsonProperty("address")]
        public string Address { get; set; }
    

    However, in the JSON, "address" is a complex nested object:

        "address": {
          "display_text": "6601 Allen Rd, Allen Park, MI 48101, United States",
          "street": "6601 Allen Road",
          "house": "6601",
          "zipcode": "48101",
          "city": "Allen Park",
          "state": "Michigan",
          "state_abbr": "MI",
          "country": "United States",
          "country_code": "US"
        }
    

    Thus Address must be as well. What's a bit odd is that you actually have a class Address - but you are not using it. Thus your Location needs to be:

    public class Location
    {
        [JsonProperty("lat")]
        public double Latitude { get; set; }
    
        [JsonProperty("lon")]
        public double Longitude { get; set; }
    
        [JsonProperty("address")]
        // Make this an Address not a string
        public Address Address { get; set; }
    }
    

    fiddle.