我已经搜索了许多网站,并将各种解决方案部署到我的Web服务器上,只是发现到目前为止我遇到的任何事情都实际上有效。我有以下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}"
]
我的模型看起来如下:
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; }
}
,我的控制器方法是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();
}
我收到的错误消息:
无法从System.String施放或转换为Models.blog.blogvm
我想念什么?另外,FWIW JSON2CSHARP不起作用。我通过的所有json都失败了。...但是,如果我在jsonlint.com上验证儿子,它将通过验证测试。
我对这里缺少的东西感到非常困惑。我只是错误地使用框架吗?
我认为您的JSON不如您想象的那么好。由于在各个地方有一些错误的报价,我无法得到您发布的验证。
我通过此JSON格式化了您发布的内容,它给了我各种错误,包括它期望的字符串的错误。这是因为一开始就具有["{
。当值得注意时,它会期望撑杆({
),但您给它报价("
),因此是错误消息。您有一些像这样的地方,可以预期撑杆,但可以找到一句。
这是一些清理干净的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
}
]
您问题的真正根源是那个不良形式的JSON来自哪里。