c# - deserialize json object does not work -
i want deserialize json object student class
var result = jsonconvert.deserializeobject<student>(data);
my json data
{ "student":{ "fname":"997544", "lname":"997544", "subject":"it", "grade":"f" } }
my student class
[serializable] public class student { [jsonproperty("fname")] public string firstname{ get; set; } [jsonproperty("lname")] public string lastname{ get; set; } [jsonproperty("subject")] public string subject { get; set; } [jsonproperty("grade")] public string grade { get; set; } }
the code not work, error says:
cannot deserialize current json object (e.g. {"name":"value"}) type because type requires json array (e.g. [1,2,3]) deserialize correctly.
if have use downloaded json need create model class it
[serializable] public class student { [jsonproperty("fname")] public string firstname{ get; set; } [jsonproperty("lname")] public string lastname{ get; set; } [jsonproperty("subject")] public string subject { get; set; } [jsonproperty("grade")] public string grade { get; set; } } [serializable] public class newmodel { public student student { get; set; } }
then deserialize
var result = jsonconvert.deserializeobject<newmodel>(data);
Comments
Post a Comment