【问题标题】:Error converting value to type 'System.Collections.Generic.List` in my json在我的 json 中将值转换为类型“System.Collections.Generic.List”时出错
【发布时间】:2019-10-18 19:23:45
【问题描述】:

我在将 JSON 转换为 C# 中的对象时遇到问题。我想知道我的 json 或对象结构有什么问题?为什么会失败?

我的 JSON 是:

[ { "added_on" : "10-03-2014",
    "country" : "INDIA",
    "crew" : "{\"Actor1\": \"Kangana\", \"Actor2\":\"Lisa Haydon\"}\r\n",
    "fb_link" : "https://www.facebook.com/Queenthefilm",
    "genre" : "DRAMA",
    "id" : 1,
    "image_first_url" : "",
    "image_main_url" : "133avk38CcBfNHsqwr0nzqOa8Db.jpg",
    "image_second_url" : "",
    "name" : "Queen",
    "ratings" : "[{\"type\": \"IMDB\", \"value\": \"9.3\"}, {\"type\": \"TOI\", \"value\": \"4\"}, {\"type\": \"NDTV\", \"value\": \"3\"}]",
    "release_date" : "07-03-2014",
    "release_year" : 2014,
    "storyline" : "A Delhi girl from a traditional family sets out on a solo honeymoon after her marriage gets canceled.",
    "twitter_link" : "https://twitter.com/Queenthefilm",
    "youtube_link" : "http://www.youtube.com/watch?v=KGC6vl3lzf0"
  } ]

我的对象结构:

public class Movie
{
    public string id { get; set; }
    public string name { get; set; }
    public string storyline { get; set; }
    public string crew { get; set; }
    public string genre { get; set; }
    public string release_year { get; set; }
    public string release_date { get; set; }
    public string country { get; set; }
    public string youtube_link { get; set; }
    public string fb_link { get; set; }
    public string twitter_link { get; set; }
    public string image_main_url { get; set; }
    public string image_first_url { get; set; }
    public string image_second_url { get; set; }
    public string added_on { get; set; }
    public List<Rating> ratings { get; set; }
}

public class Rating
{
    public string type { get; set; }
    public string value { get; set; }
}

错误:

Error converting value "[{"type": "IMDB", "value": "9.3"}, {"type": "TOI", "value": "4"}, {"type": "NDTV", "value": "3"}]" to type 'System.Collections.Generic.List`1[mq.channel.Rating]'. Path '[0].ratings', line 1, position 731.

【问题讨论】:

  • 在使用 EF Core 将字符串化的 JSON 反序列化到/从数据库中时遇到此错误。 EF Core 已从我预计将被删除的列中复制了数据,并且旧数据与预期格式不匹配。

标签: c# json json.net


【解决方案1】:

您的 JSON 不包含 ratings 属性的数组,而是一个字符串。 它应该看起来像

[ { "added_on" : "10-03-2014",
    "country" : "INDIA",
    "crew" : "{\"Actor1\": \"Kangana\", \"Actor2\":\"Lisa Haydon\"}\r\n",
    "fb_link" : "https://www.facebook.com/Queenthefilm",
    "genre" : "DRAMA",
    "id" : 1,
    "image_first_url" : "",
    "image_main_url" : "133avk38CcBfNHsqwr0nzqOa8Db.jpg",
    "image_second_url" : "",
    "name" : "Queen",
    "ratings" : [{"type": "IMDB", "value": "9.3"}, {"type": "TOI", "value": "4"}, {"type": "NDTV", "value": "3"}],
    "release_date" : "07-03-2014",
    "release_year" : 2014,
    "storyline" : "A Delhi girl from a traditional family sets out on a solo honeymoon after her marriage gets canceled.",
    "twitter_link" : "https://twitter.com/Queenthefilm",
    "youtube_link" : "http://www.youtube.com/watch?v=KGC6vl3lzf0"
  } ]

为了将ratings 属性作为数组进行分析(注意引号和转义反斜杠已被删除)

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,我用下面的代码解决了

    YourVariable.Replace("\"[", "[").Replace("]\"", "]").Replace("\\", ""); 
    

    【讨论】:

      【解决方案3】:

      您需要从 ratings 数组属性中删除引号,否则它将被视为字符串,即

      "ratings": [...]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-11
        • 1970-01-01
        • 2021-11-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多