Reputation: 3102
I have a json response that I am trying to deserialize with RestSharp This is how it looks
[{"cname":"APPLICANT NAME","dob":"01-01-1997","gender":"2","msg":"Ok","msgcode":"0"},
{"results":
[{"subjectcode":"214","subject":"SOCIAL STUDIES","grade":"C5"},
{"subjectcode":"302","subject":"ENGLISH LANG","grade":"C5"},
{"subjectcode":"402","subject":"MATHEMATICS(CORE)","grade":"E8"},
{"subjectcode":"517","subject":"INTEGRATED SCIENCE","grade":"D7"},
{"subjectcode":"203","subject":"ECONOMICS","grade":"D7"},
{"subjectcode":"204","subject":"GEOGRAPHY","grade":"B3"},
{"subjectcode":"205","subject":"GOVERNMENT","grade":"C6"},
{"subjectcode":"207","subject":"HISTORY","grade":"B3"}]
}]
I have created these classes to help me serialize
public class Result
{
public string subjectcode { get; set; }
public string subject { get; set; }
public string grade { get; set; }
}
public class StudentDetail
{
public string cname { get; set; }
public string dob { get; set; }
public string gender { get; set; }
public string msg { get; set; }
public string msgcode { get; set; }
public List<Result> results { get; set; }
}
public class VerifyData
{
public List<StudentDetail> StudentDetails { get; set; }
}
However whenever I try to deserialize with RestSharp
IRestResponse response = client.Execute<VerifyData>(request);
I get the following error message:
{"Unable to cast object of type 'RestSharp.JsonArray' to type 'System.Collections.Generic.IDictionary`2[System.String,System.Object]'."}
Any pointers, have searched similar questions but was not able to fix this. Thanks
Json data now is
[{"cname":"APPLICANT NAME","dob":"01-01-1997","gender":"2","msg":"Ok","msgcode":"0",
"results":
[{"subjectcode":"214","subject":"SOCIAL STUDIES","grade":"C5"},
{"subjectcode":"302","subject":"ENGLISH LANG","grade":"C5"},
{"subjectcode":"402","subject":"MATHEMATICS(CORE)","grade":"E8"},
{"subjectcode":"517","subject":"INTEGRATED SCIENCE","grade":"D7"},
{"subjectcode":"203","subject":"ECONOMICS","grade":"D7"},
{"subjectcode":"204","subject":"GEOGRAPHY","grade":"B3"},
{"subjectcode":"205","subject":"GOVERNMENT","grade":"C6"},
{"subjectcode":"207","subject":"HISTORY","grade":"B3"}]
}]
Upvotes: 1
Views: 98
Reputation: 53958
You don't need the extra class VerifyData
. That you need is the following:
var studentDetails = client.Execute<StudentDetail>(request).Data;
or in two steps:
var response = client.Execute<StudentDetail>(request);
var studentDetails = response.Data;
UPDATE
Using JsonConvert
(Json.NET), if you defined the following classes:
public class Result
{
[JsonProperty("subjectcode")]
public string Subjectcode { get; set; }
[JsonProperty("subject")]
public string Subject { get; set; }
[JsonProperty("grade")]
public string Grade { get; set; }
}
public class StudentDetail
{
[JsonProperty("cname")]
public string Cname { get; set; }
[JsonProperty("dob")]
public string Dob { get; set; }
[JsonProperty("gender")]
public string Gender { get; set; }
[JsonProperty("msg")]
public string Msg { get; set; }
[JsonProperty("msgcode")]
public string Msgcode { get; set; }
[JsonProperty("results")]
public IList<Result> Results { get; set; }
}
You can try the following one:
JsonConvert.Deserialize<StudentDetail>(json)
Upvotes: 2