0

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

3
  • What does the Json string look like? Commented Nov 24, 2014 at 21:41
  • json added to the main Commented Nov 24, 2014 at 21:50
  • Did you try adding a default constructor in your IMDB class? Commented Nov 24, 2014 at 22:34

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you but it does not allow me to use "item". Because it does not exist.
Now when you have included the actual JSON in your question, my answer doesn't reflect that. Will update later.
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.