【问题标题】:Unable to Parse JSON using NewtonSoft JSONConvert无法使用 NewtonSoft JSONConvert 解析 JSON
【发布时间】:2017-03-19 09:07:38
【问题描述】:

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

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

【问题讨论】:

    标签: c# json.net json-deserialization


    【解决方案1】:

    您的问题是,在您的 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.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-16
      • 2020-04-12
      • 2014-08-14
      • 1970-01-01
      相关资源
      最近更新 更多