1

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"}]
}]

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your suggestion, Just tried your suggestion and it returns null
@Tonto You are welcome. Have you added this client.AddHandler("application/json", () => new RestSharp.Serializers.Newtonsoft.Json.NewtonsoftJsonSerializer()); for your client ? This should be done before we call the Execute method.
thanks for your time. I am unable to add the line you suggested without an error. I am playing around with JsonConvert.Deserialize as well and this seems to give some results. If I do JsonConvert.Deserialize<StudentDetail>(json) it gives me an error. But if i do JsonConvert.Deserialize<List<StudentDetail>(json) the serialization is done but return 2 items instead of just 1
ok, this JsonConvert.Deserialize<List<StudentDetail>>(json) gives me the correct data now. The json was not properly formed earlier so we had to talk with the API designers about it. Now I ended up with this which works JsonConvert.DeserializeObject<List<StudentDetail>>(jsonContent)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.