简体   繁体   English

C#中的JSON反序列化错误

[英]JSON Deserialization Error in C#

I want to Deserialize a JSON object to C# but I'm getting this exception: 我想将一个JSON对象反序列化为C#,但我得到了这个异常:

Cannot deserialize the current JSON array (eg [1,2,3]) into type 'FYP___Task_1.RootObject' because the type requires a JSON object (eg {"name":"value"}) to deserialize correctly. 无法将当前JSON数组(例如[1,2,3])反序列化为类型'FYP ___ Task_1.RootObject',因为该类型需要JSON对象(例如{“name”:“value”})才能正确反序列化。

To fix this error either change the JSON to a JSON object (eg {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (eg ICollection, IList) like List that can be deserialized from a JSON array. 要修复此错误,请将JSON更改为JSON对象(例如{“name”:“value”})或将反序列化类型更改为数组或实现集合接口的类型(例如ICollection,IList),例如List从JSON数组反序列化。 JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. JsonArrayAttribute也可以添加到类型中以强制它从JSON数组反序列化。

Path '', line 1, position 1. 路径'',第1行,第1位。

I've tried to get this error solved by different solutions I found on StackOverflow but no one worked. 我试图通过我在StackOverflow上找到的不同解决方案来解决此错误,但没有人工作。

The JSON I'm using is as follows: 我正在使用的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": "Transporter\n \"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/"
    }
]

The classes I'm using: 我正在使用的课程:

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; }
    }

Your JSON object has the structure [ {..} ] which means it is a list of objects. 您的JSON对象具有结构[ {..} ] ,这意味着它是一个对象列表。 In your case, your list has only one object, but it is still a list. 在您的情况下,您的列表只有一个对象,但它仍然是一个列表。 What you are trying to do is turn the list into an object, so you get an exception. 你要做的是将列表变成一个对象,所以你得到一个例外。

The solution is either to change your JSON to {..} (ie remove the square brackets) OR deserialize the JSON into an array of RootObject and then just read the first one, for example: 解决方案是将您的JSON更改为{..} (即删除方括号)或将JSON反序列化为RootObject数组,然后只读取第一个,例如:

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

We can try the below option also. 我们也可以尝试以下选项。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM