c# - 无法使用 NewtonSoft JSONConvert 解析 JSON

标签 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."}

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

最佳答案

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

关于c# - 无法使用 NewtonSoft JSONConvert 解析 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42881541/

相关文章:

c# - 网络 :MaxJsonDeserializerMembers vs maxRequestLength

c# - LINQ中的include方法用于Left Join?

c# - 路线模式与单独路线

c# - 有人怎么能像 Java 一样制作 C# 增量编译器?

Json.NET 需要反序列化的所有属性

c# - DataTable反序列化后DateTime列类型变为String类型

c# - 反射——从类型到类(不是类的实例)

c# - 计算物体摩擦力时不准确

c# - 将 WCF 4 中的默认 JSON 序列化程序替换为 JSON.NET

c# - Newtonsoft JSON - 反序列化JSON时如何使用JsonConverter.ReadJson方法进行类型转换