Reputation: 1
I have an HTTP call that returns the below, and I am trying to convert the coversio_rates into a returnable model, some of the values get populated, but converio_rates are always null.
string response = apiResponse.Content.ReadAsStringAsync().Result;
var typetoReturn = JsonConvert.DeserializeObject<Info>(response);
but the values never the set.
API response model example
{
"result": "success",
"documetatio": "https: //www.exchagerate-api.com/docs",
"terms_of_use": "https://www.exchagerate-api.com/terms",
"time_last_update_uix": 1631266201,
"time_last_update_utc": "Fri, 10 Sep 2021 09:30:01 +0000",
"time_ext_update_uix": 1631352601,
"time_ext_update_utc": "Sat, 11 Sep 2021 09:30:01 +0000",
"base_code": "USD",
"coversio_rates": {
"USD": 1,
"AED": 3.6725,
"AF": 81.8475,
"ALL": 102.7736,
"AMD": 492.9916,
}
}
Model Coversio_Rates
public class Coversio_Rates
{
public float USD { get; set; }
public float AED { get; set; }
public float AF { get; set; }
public float ALL { get; set; }
public float AMD { get; set; }
}
public class Info
{
public string result { get; set; }
public string documetatio { get; set; }
public string base_code { get; set; }
public Coversio_Rates Coversio_Rates { get; set; }
}
Upvotes: 0
Views: 1487
Reputation: 118937
You need a wrapper class since those properties are child values. For example:
public class Root
{
public Coversio_Rates Coversio_Rates { get; set; }
// Add in the other properties if you want them too
}
Now do:
var typetoReturn = JsonConvert.DeserializeObject<Root>(response);
Some other points:
.Result
, instead you should await
the task.CoversioRates
as that is a more common best practice in C# code. Same goes for the property name, but you will need to add a [JsonProperty("Coversio_Rates")]
attribute.Upvotes: 1