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?
I don't think your JSON is as good as you think it is. I couldn't get what you posted to pass validation, because of some errant quotes in various places.
I ran what you posted through this JSON formatter , and it gave me all kinds of errors, including the error that it was expecting a string. This is because of the ["{
at the very beginning. When deserializing, it expects a brace ( {
), but you gave it a quote ( "
), hence the error message. You have a few places like this where a brace is expected, but a quote is found.
Here is some cleaned up JSON. This should deserialize just fine.
[
{
"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
}
]
The real root of your problem is where that ill-formatted JSON came from.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.