C# 无法将当前JSON对象(例如,{quot;name";:“value";})反序列化为类型';ASP.NET MVC中的System.Collections.Generic.List`1
我试图从Json Rest API获取数据,并使用ASP.NET MVC在视图中显示。我遵循了教程 我收到一个错误,提示“无法将当前JSON对象(例如,{“名称”:“值”})反序列化为类型”System.Collections.Generic.List`1[diversity.Models.Species]”,因为该类型需要JSON数组(例如[1,2,3])才能正确反序列化。 若要修复此错误,请将JSON更改为JSON数组(例如[1,2,3]),或更改反序列化类型,使其成为可以从JSON对象反序列化的正常.NET类型(例如,不是integer之类的基元类型,也不是数组或列表之类的集合类型)。还可以将JsonObjectAttribute添加到类型中,以强制它从JSON对象反序列化。 路径'userId',第2行,位置12' 我知道其他相关问题,例如。但是,这建议将C# 无法将当前JSON对象(例如,{quot;name";:“value";})反序列化为类型';ASP.NET MVC中的System.Collections.Generic.List`1,c#,json,asp.net-mvc,C#,Json,Asp.net Mvc,我试图从Json Rest API获取数据,并使用ASP.NET MVC在视图中显示。我遵循了教程 我收到一个错误,提示“无法将当前JSON对象(例如,{“名称”:“值”})反序列化为类型”System.Collections.Generic.List`1[diversity.Models.Species]”,因为该类型需要JSON数组(例如[1,2,3])才能正确反序列化。 若要修复此错误,请将JSON更改为JSON数组(例如[1,2,3]),或更改反序列化类型,使其成为可以从JSON对象反序
替换为
。如果我这样做,它会导致视图中有关@model IEnumerable
的错误
我认为api的响应不是列表,而是单个对象那么我如何才能正确地实现这一点?
如果将来我必须得到一个列表,我应该如何更改代码
这是我的密码
SpeciesController.cs
SpeciesDetails.cshtml
@model IEnumerable
@{
ViewBag.Title=“SpeciesDetails”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
物种详情
@ActionLink(“新建”、“创建”)
@DisplayNameFor(model=>model.UserId)
@DisplayNameFor(model=>model.Title)
@DisplayNameFor(model=>model.Body)
@foreach(模型中的var项目){
@DisplayFor(modelItem=>item.UserId)
@DisplayFor(modeleItem=>item.Title)
@DisplayFor(modelItem=>item.Body)
@ActionLink(“编辑”,“编辑”,新的{id=item.id})|
@ActionLink(“详细信息”,“详细信息”,新的{id=item.id})|
@ActionLink(“删除”,“删除”,新的{id=item.id})
}
datalist=新列表{JsonConvert.DeserializeObject(responseBody)}代码>它对MJ Wills有效
using diversity.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LinqToWiki.Download;
using LinqToWiki.Generated;
using LinqToWiki;
using System.Text;
using RestSharp;
using Newtonsoft.Json.Linq;
using System.Net.Http;
using Newtonsoft.Json;
namespace diversity.Controllers
{
public class SpeciesController : Controller
{
public async System.Threading.Tasks.Task<ActionResult> SpeciesDetails(){
HttpClient client = new HttpClient();
List<Species> datalist = new List<Species>();
try
{
HttpResponseMessage response = await client.GetAsync("https://jsonplaceholder.typicode.com/posts/1");
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
datalist = JsonConvert.DeserializeObject<List<Species>>(responseBody);
}
catch (HttpRequestException e)
{
System.Diagnostics.Debug.WriteLine("\nException Caught!");
System.Diagnostics.Debug.WriteLine("Message :{0} ", e.Message);
}
client.Dispose();
return View(datalist);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace diversity.Models
{
public class Species
{
public int UserId { get; set; }
public int Id { get; set; }
public string Title { get; set; }
public string Body { get; set; }
}
}
@model IEnumerable<diversity.Models.Species>
@{
ViewBag.Title = "SpeciesDetails";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>SpeciesDetails</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.UserId)
</th>
<th>
@Html.DisplayNameFor(model => model.Title)
</th>
<th>
@Html.DisplayNameFor(model => model.Body)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.UserId)
</td>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.Body)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
@Html.ActionLink("Details", "Details", new { id=item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
}
</table>