Reputation: 99
I have a JavaScriptSerializer() that deserialize a JSON from a web service and fails on runtime due to a wrong handling of a list.
This is my class:
public class LoginReply
{
public Userdata userdata { get; set; }
public List<string> errors { get; set; }
}
public class Userdata
{
public string username { get; set; }
public string section{ get; set; }
public string avatar { get; set; }
public string description { get; set; }
public string authkey { get; set; }
}
And I deserilize with:
LoginReply reply = new JavaScriptSerializer().Deserialize<LoginReply>(loginRespose);
I also tried with:
LoginReply reply = new JavaScriptSerializer().Deserialize<List<LoginReply>>(loginRespose);
without any success
(Cannot implicitly convert type 'System.Collections.Generic.List' to 'Redacted.Redacted.LoginReply')
Upvotes: 0
Views: 33
Reputation: 4002
The problem relies in here:
LoginReply reply = new JavaScriptSerializer().Deserialize<List<LoginReply>>(loginRespose);
Youa re trying to deserialize the loginRespose to a list of LoginReply but in other side you are declaring it not as a list.
For the first attemp we have to see the json assigned to loginRespose variable because it might be null or not valid json or does not have the properties like LoginReply
class.
If the json is coming like a list of LoginReplys
you might have to tro deserializing it to a list of this class like below:
List<LoginReply> reply = new JavaScriptSerializer().Deserialize<List<LoginReply>>(loginRespose);
Upvotes: 1