无法使用NewtonSoft JSONConvert解析JSON

时间:2017-03-19 00:33:55

标签: c# json.net json-deserialization

我正在尝试反序列化以下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
    }
  ]
}

我在.NET中使用Newtonsoft.JSON。

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

我的班级映射如下。

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

我收到带有消息

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

请帮我解决这个问题。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:3)

您的问题是,在Location课程中,您拥有以下属性:

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

但是,在JSON中,"address"是一个复杂的嵌套对象:

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

因此Address也必须如此。有点奇怪的是,你实际上有一个班级Address - 但你没有使用它。因此,您的Location必须是:

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