C#中的JSON反序列化错误



我想将JSON对象反序列化到C#,但我得到了这个异常:

无法将当前JSON数组(例如[1,2,3])反序列化为类型"FYP___Task_1.RootObject",因为该类型需要JSON对象(例如{"name":"value"})才能正确反序列化。

若要修复此错误,请将JSON更改为JSON对象(例如{"name":"value"}),或将反序列化的类型更改为数组或实现集合接口(例如ICollection、IList)的类型,例如可以从JSON数组反序列化的List。JsonArrayAttribute也可以添加到类型中,以强制它从JSON数组反序列化。

路径",第1行,位置1。

我试图通过在StackOverflow上找到的不同解决方案来解决这个错误,但没有人成功。

我使用的JSON如下:

[
    {
        "rating_count": 158271,
        "genres": [
            "Action",
            "Crime",
            "Thriller"
        ],
        "rated": "PG-13",
        "language": [
            "English",
            "French",
            "Mandarin"
        ],
        "rating": 6.7,
        "country": [
            "France",
            "USA"
        ],
        "release_date": 20021011,
        "title": "Transportern "The Transporter"",
        "year": 2002,
        "filming_locations": "Avenue de Saissy, Cannes, Alpes-Maritimes, France",
        "imdb_id": "tt0293662",
        "directors": [
            "Corey Yuen"
        ],
        "writers": [
            "Luc Besson",
            "Robert Mark Kamen"
        ],
        "actors": [
            "Jason Statham",
            "Qi Shu",
            "Matt Schulze",
            "François Berléand",
            "Ric Young",
            "Doug Rand",
            "Didier Saint Melin",
            "Tonio Descanvelle",
            "Laurent Desponds",
            "Matthieu Albertini",
            "Vincent Nemeth",
            "Jean-Yves Bilien",
            "Jean-Marie Paris",
            "Adrian Dearnell",
            "Alfred Lot"
        ],
        "also_known_as": [
            "Transporter"
        ],
        "poster": {
            "imdb": "http://ia.media-imdb.com/images/M/MV5BMTk2NDc2MDAxN15BMl5BanBnXkFtZTYwNDc1NDY2._V1_SY317_CR3,0,214,317_.jpg",
            "cover": "http://imdb-poster.b0.upaiyun.com/000/293/662.jpg!cover?_upt=cd37cf0e1385015165"
        },
        "runtime": [
            "92 min"
        ],
        "type": "M",
        "imdb_url": "http://www.imdb.com/title/tt0293662/"
    }
]

我正在使用的类:

public class Poster
    {
        public string imdb { get; set; }
        public string cover { get; set; }
    }
    public class RootObject
    {
        public int rating_count { get; set; }
        public List<string> genres { get; set; }
        public string rated { get; set; }
        public List<string> language { get; set; }
        public double rating { get; set; }
        public List<string> country { get; set; }
        public int release_date { get; set; }
        public string title { get; set; }
        public int year { get; set; }
        public string filming_locations { get; set; }
        public string imdb_id { get; set; }
        public List<string> directors { get; set; }
        public List<string> writers { get; set; }
        public List<string> actors { get; set; }
        public List<string> also_known_as { get; set; }
        public Poster poster { get; set; }
        public List<string> runtime { get; set; }
        public string type { get; set; }
        public string imdb_url { get; set; }
    }

JSON对象的结构为[ {..} ],这意味着它是一个对象列表。在您的情况下,您的列表只有一个对象,但它仍然是一个列表。您要做的是将列表变成一个对象,这样您就会得到一个异常。

解决方案是将JSON更改为{..}(即删除方括号),或者将JSON反序列化为RootObject的数组,然后只读取第一个,例如:

RootObject[] myArray = json.Deserialize<RootObject[]>("json goes here");
RootObject firstObject = myArray[0];

我们也可以尝试下面的选项。

var root = response1.Content.ReadAsAsync<RootObject>().Result;           
GridView1.DataSource = root.items;

相关内容

  • 没有找到相关文章

最新更新