Jonathan Small
Jonathan Small

Reputation: 1079

JsonConvert.DeserializeObject returns nulls

I have the following code which I am using in an ASP.NET MVC c# application

HttpClient client = new HttpClient();
try
{
    var result = client.GetStringAsync(url).Result;
    APIReturn = JsonConvert.DeserializeObject<APIReturn>(result);
}
catch
{
}

When I place my breakpoint on the APIReturn = .... line, and I view the contents of result, I see what looks like a valid return from the API call. I even copied the contents of the variable result and applied it to an online json tool. This is what the tool shows: enter image description here

This is the definition of the APIReturn class:

public class APIReturn
{
    public string return_response { get; set; }
    public string return_code { get; set; }
    public string return_plan_name { get; set; }
    public string return_menu_string { get; set; }
    public string return_peo_ind { get; set; }
}

At the end of the execution of the code, I look at the values of APIReturn and each field is null.

Any ideas why Json is not parsing the string?

Thank you.

Upvotes: 0

Views: 273

Answers (1)

Jawad
Jawad

Reputation: 11364

Your json is an array of objects... but you are deserializing it to an object. Change the deserialization to List and should work

var list = JsonConvert.DeserializeObject<List<APIReturn>>(result);

Upvotes: 1

Related Questions