Error converting value to type 'System.Collections.Generic.List` in my json

一个人想着一个人 提交于 2019-12-10 12:25:57

问题


I am having problem in converting JSON to object in C#. I want to know that what's wrong with my json or object structure? Why its failing?

My JSON is:

[ { "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"
  } ]

My object structure:

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:

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.

回答1:


Your JSON doesn't contain an array for the ratings property but a string. It should rather look like

[ { "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"
  } ]

in order for the ratings property to be analyzed as an array (note that the quotes and escape backslashes are removed)




回答2:


You need to remove the quotes from your ratings array property, otherwise it's going to be treated as a string i.e.

"ratings": [...]



回答3:


I met with same problem and i solved with following code

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


来源:https://stackoverflow.com/questions/22451259/error-converting-value-to-type-system-collections-generic-list-in-my-json

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!