JSON deserialization error in C #

I want to deserialize a JSON object in C #, but I get this exception:

It is not possible to deserialize the current JSON array (for example, [1,2,3]) to type 'FYP ___ Task_1.RootObject' because it requires a JSON object (for example, {"name": "value"}) for deserialization.

To fix this error, either change the JSON to a JSON object (for example, {"name": "value"}), or change the deserialized type to an array or a type that implements the collection interface (for example, ICollection, IList), for example, a List that can be deserialize from a JSON array. JsonArrayAttribute can also be added to a type to make it deserialize from a JSON array.

Path '', line 1, position 1.

I tried to solve this error with various solutions found in StackOverflow, but no one worked.

The JSON I am using is as follows:

[
    {
        "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 use are:

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; }
    }
+4
source share
2 answers

Your JSON object has a structure [ {..} ], which means it is a list of objects. 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 to change the JSON to {..}(i.e. remove the square brackets) OR deserialize the JSON into an array RootObject, and then just read the first one, for example:

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

.

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

All Articles