I am calling my web api from my C# desktop client.
This is my code on the client:
public IEnumerable<Model.Print> Get()
{
var print = new List<Model.Print>();
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(Shared.URL);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(Shared.HeaderType));
var response = client.GetAsync(route + "?" + GeneralTags.CUSTOMER_REF + "=" + new Guid().ToString()).Result;
if (response.IsSuccessStatusCode)
{
string strJson = response.Content.ReadAsStringAsync().Result;
var jObj2 = (IEnumerable<Model.Print>)JsonConvert.DeserializeObject(strJson);
}
else
{
evError(new Exception(String.Format("{0}: {1}", (int)response.StatusCode, response.ReasonPhrase)), ErrorTags.PRINT_GET);
}
}
return print;
}
But I get an error on this line:
var jObj2 = (IEnumerable<Model.Print>)JsonConvert.DeserializeObject(strJson);
The error is:
'Newtonsoft.Json.Linq.JArray' to type 'System.Collections.Generic.IEnumerable`1[InformedWorkerServer.Model.Print]'.
The strJson contains a value:
[
{"PrintId":1,"PrintRef":"00000000-0000-0000-0000-000000000000","Header":"header","TC":"tc","CompanyRef":"00000000-0000-0000-0000-000000000000"},
{"PrintId":2,"PrintRef":"39a10cee-7cb3-4ed3-aec2-293761eed96d","Header":"header","TC":"tc","CompanyRef":"00000000-0000-0000-0000-000000000000"}]
What am i missing?
Use JsonConvert.DeserializeObject<T>
instead:
var jObj2 = JsonConvert.DeserializeObject<List<Model.Print>>(strJson);
If you would like to format the nested json structure as represented in the image below. you can make use of the below formatter in Newtonsoft.Json.
Solution : var _addressDetails = JsonConvert.DeserializeObject>(Convert.ToString(data.addressDetails));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With