No parameterless constructor defined for type

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:Usersahmetozsaridocumentsvisual studio 2010Proje ctsimdb_io_webimdb_io_webWebForm1.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


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.


Firstly you can use HttpClient class for API operations.

 using (var httpClient = new HttpClient())
            {
                var operationResult = await httpClient.GetStringAsync(@"http://localhost/api/requests");

            }

Secondly, for JSON conversion operations you can use Json.NET

using Newtonsoft.Json;

public class RequestJson
{
    [JsonProperty("request")]
    public Request Request { get; set; }
}

public class Request
{
    [JsonProperty("name")]
    public string Name{ get; set; }

}

JsonConvert.DeserializeObject<List<RequestJson>>(operationResult );

You have to use List template type because with my understanding when you are creating an array type then Newtonsoft.Json would not be able to instantiate the object because it needs a size.

链接地址: http://www.djcxy.com/p/27988.html

上一篇: 无法识别的Guid格式在线项目错误,

下一篇: 没有为类型定义的无参数构造函数