Json – No parameterless constructor defined for type

asp.netdeserializationjson

I am trying to learn and to do something with asp.net. I'm trying to get some info from json api using import.io's api, but i could not figure out something. I am trying to solve it till 2 days:

ERROR : No parameterless constructor defined for type of 'imdb_io_web.IMDB[]'

Why am I getting that error I really don't understand?

I have a class

namespace imdb_io_web
{
    public class IMDB
    {
        public string director { get; set; } 
    }
}

and trying to get director name from IMDB

var wc = new WebClient();
        var serializer = new JavaScriptSerializer();
        var result = serializer.Deserialize<IMDB[]>(wc.DownloadString("MYAPI"));
        foreach (var item in result) { Label1.Text = item.director; }

[MissingMethodException: No parameterless constructor defined for type of 'imdb_io_web.IMDB[]'.]
System.Web.Script.Serialization.ObjectConverter.ConvertDictionaryToObject(IDictionary`2 dictionary, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject) +527729
System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject) +66
System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject) +145
System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit) +66
System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(String input) +70
imdb_io_web.WebForm1.Page_Load(Object sender, EventArgs e) in C:\Users\ahmetozsari\documents\visual studio 2010\Projects\imdb_io_web\imdb_io_web\WebForm1.aspx.cs:26
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
System.Web.UI.Control.OnLoad(EventArgs e) +92
System.Web.UI.Control.LoadRecursive() +54
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772

Best Solution

Either you deserialize a single element:

 var result = serializer.Deserialize<IMDB>(wc.DownloadString("MYAPI"));
 Label1.Text = item.director;

or a list

 var result = serializer.Deserialize<List<IMDB>>(wc.DownloadString("MYAPI"));
 foreach (var item in result) { Label1.Text = item.director; }

The array type (IMDB[]), as you read in the exception, cannot be used as a type parameter for the deserializer as it lacks the parameterless constructor. Using List<IMDB> should possibly resolve the issue.

Related Question