JSON 将值转换为类型时出错



反序列化此 JSON 文件时出错

{
  "checkOut": "10:30",
  "stars": 4,
  "locationId": 953,
  "propertyType": 6,
  "checkIn": "15:00",
  "trustyou": {
    "languageSplit": [
      {
        "tripTypeSplit": [
          {
            "type": "family",
            "percentage": 85
          },
          {
            "type": "couple",
            "percentage": 15
          }
        ],
        "name": "de",
        "percentage": 100
      }
    ],
    "location": [
    ],
    "reviewsCount": 83,
    "popularity": 0,
    "tripTypeSplit": [
      {
        "type": "family",
        "percentage": 86
      },
      {
        "type": "couple",
        "percentage": 14
      }
    ],
    "sentimentScoreList": [
      {
        "categoryId": "14",
        "ratio": "Good",
        "shortText": "Great location",
        "name": "Location",
        "subcategories": [
        ],
        "highlights": [
          {
            "text": "Beautiful location",
            "confidence": 100
          }
        ],
        "reviewCount": 14,
        "score": 100
      },
      {
        "categoryId": "111",
        "ratio": "Good",
        "shortText": "Rather comfortable",
        "name": "Comfort",
        "subcategories": [
        ],
        "highlights": [
        ],
        "reviewCount": 5,
        "score": 100
      },

我有以下适用于此 JSON 的类

public class Root
    {
        [JsonProperty("checkIn")]
        public string CheckIn { get; set; }
        [JsonProperty("distance")]
        public double Distance { get; set; }
        [JsonProperty("hidden")]
        public bool Hidden { get; set; }
        [JsonProperty("trustyou")]
        public Trustyou Trustyou { get; set; }
        [JsonProperty("amenitiesV2")]
        public AmenitiesV2 AmenitiesV2 { get; set; }
        [JsonProperty("hasAirbnb")]
        public bool HasAirbnb { get; set; }
        [JsonProperty("checkOut")]
        public string CheckOut { get; set; }
        [JsonProperty("popularity")]
        public int Popularity { get; set; }
        [JsonProperty("name")]
        public string Name { get; set; }
        [JsonProperty("id")]
        public int Id { get; set; }
        [JsonProperty("cntRooms")]
        public int CntRooms { get; set; }

似乎有什么问题? 我正在使用反序列化它

    string resp2 = await client.GetStringAsync("");
    var hotelDetails = JsonConvert.DeserializeObject<IDictionary<string, HotelsDescriptionAPI.Root>>(resp2, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
    foreach (var hoteldesc in hotelDetails)
    {
        MessageBox.Show(hoteldesc.Value.Id);
    }    

确切的错误是

"Error converting value 24545 to type and  Error converting value "10:30" to type 'HotelsDescriptionAPI.Root'. Path 'checkOut', line 1, position 19."

我试图获取"Id"的值,我的代码可能有什么问题?

反序列化代码应为:

var hotelDetails = JsonConvert.DeserializeObject<HotelsDescriptionAPI.Root>(resp2, 
                   new JsonSerializerSettings { 
                       NullValueHandling = NullValueHandling.Ignore 
                   });

您正在尝试将其反序列化为string,Root字典,而对象本身只是Root

它似乎不适用于你的方案,但请注意,当你的 JSON 是一个数组(根级子级是数组项,而不是属性(时,你可能必须将根对象更改为兼容类型的子类。

例如:

public class RootObject : List<ChildObject>
{
}
public class ChildObject
{
    public string Property1 { get; set; }
    public string Property2 { get; set; }
}

对于这没有帮助,并且没有使用实体框架和/或手动编写域类的人 - 确保所有类属性都与数据源中的内容以相同的确切字段顺序匹配。

相关内容

  • 没有找到相关文章

最新更新