I've searched many a web site, and deployed various solutions to my web server, only to find that nothing I have come across so far actually works. I have the following JSON:
["{\"EntryNo\":4,\"Title\":\"New Title\",\"Summary\":\"New Summary\",\"Body\":\"New Body\",\"CreatedOn\":\"2017-03-03T03:53:16.2955903+00:00\",\"CreatedBy\":\"Jim Jones\",\"ModifiedBy\":\"Jim Jones\",\"ModifiedOn\":\"2017-02-25T07:06:32.517\",\"Deleted\":false,\"TypeId\":1}",
"{\"EntryNo\":5,\"Title\":\"Old Title\",\"Summary\":\"Old Summary\",\"Body\":\"Old Body\",\"CreatedOn\":\"2017-03-03T03:53:16.2955903+00:00\",\"CreatedBy\":\"Ben Jones\",\"ModifiedBy\":\"Ben Jones\",\"ModifiedOn\":\"2017-02-25T07:06:32.593\",\"Deleted\":false,\"TypeId\":1}"
]
My Model looks like the following:
public class BlogVM
{
[JsonProperty(PropertyName = "EntryNo")]
public int EntryNo { get; set; }
[JsonProperty(PropertyName = "Title")]
public string Title { get; set; }
[JsonProperty(PropertyName = "Summary")]
public string Summary { get; set; }
[JsonProperty(PropertyName = "Body")]
public string Body { get; set; }
[JsonProperty(PropertyName = "CreatedOn")]
public DateTime CreatedOn { get; set; }
[JsonProperty(PropertyName = "ModifiedOn")]
public DateTime ModifiedOn { get; set; }
[JsonProperty(PropertyName = "CreatedBy")]
public string CreatedBy { get; set; }
[JsonProperty(PropertyName = "ModifiedBy")]
public string ModifiedBy { get; set; }
[JsonProperty(PropertyName = "Deleted")]
public bool Deleted { get; set; }
[JsonProperty(PropertyName = "TypeId")]
public int? TypeId { get; set; }
}
and my controller method is admittedly a hodgepodge...
public async Task<ActionResult> _ActualBlogContent()
{
HttpClient client = new HttpClient();
client.BaseAddress = new System.Uri("wwww.myapiaddresshere.com");
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.GetAsync("api/entry/").Result;
if (response.IsSuccessStatusCode)
{
var dataObjects = response.Content.ReadAsStringAsync();
List<BlogVM> pdone = JsonConvert.DeserializeObject<List<BlogVM>>(dataObjects.Result);
}
return View();
}
I'm getting an error message of:
Could not cast or convert from System.String to Models.Blog.BlogVM
What am I missing? Also, fwiw json2Csharp does not work. All json I pass it fails....But if I validate the son on jsonlint.com, it passes validation testing.
I'm really confused as to what's missing here. Am I just using the framework incorrectly?
awaiting theresponse.Content.ReadAsStringAsync().. Its the only reason why you have set the method as anasync Taskanyway. Now I would assume you are not getting the result your expecting.