Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON Deserialization Error in C#

I want to Deserialize a JSON object to C# but I'm getting this exception:

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'FYP___Task_1.RootObject' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

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

Path '', line 1, position 1.

I've tried to get this error solved by different solutions I found on StackOverflow but no one worked.

The JSON I'm 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'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; }
    }
like image 410
onepoordeveloper Avatar asked Feb 14 '23 14:02

onepoordeveloper


1 Answers

Your JSON object has the 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 either to change your JSON to {..} (i.e. remove the square brackets) OR deserialize the JSON into an array of RootObject and then just read the first one, for example:

RootObject[] myArray = json.Deserialize<RootObject[]>("json goes here");
RootObject firstObject = myArray[0];
like image 97
prestomanifesto Avatar answered Feb 24 '23 10:02

prestomanifesto