C# 从web API恢复数据[已解决]
我有一个使用asp.net core开发的API,它允许对DocumentType(文档类型)执行CRUD,接下来我有一个asp.net core mvc web应用程序,我想从中恢复DocumentType列表,以便在我使用HttpClient的两个应用程序之间进行通信。下面是允许您检索文档列表的操作代码:C# 从web API恢复数据[已解决],c#,json,asp.net-core-mvc,dotnet-httpclient,C#,Json,Asp.net Core Mvc,Dotnet Httpclient,我有一个使用asp.net core开发的API,它允许对DocumentType(文档类型)执行CRUD,接下来我有一个asp.net core mvc web应用程序,我想从中恢复DocumentType列表,以便在我使用HttpClient的两个应用程序之间进行通信。下面是允许您检索文档列表的操作代码: public async Task<IActionResult> Index() { List<DocumentType> document
public async Task<IActionResult> Index()
{
List<DocumentType> documentTypeliste = new List<DocumentType>();
using (var httpClient = new HttpClient())
{
using (var response = await httpClient.GetAsync("https://localhost:44318/api/DocumentType"))
{
var apiResponse = await response.Content.ReadAsStringAsync();
documentTypeliste = JsonConvert.DeserializeObject<List<DocumentType>>(apiResponse);
}
}
return View(documentTypeliste);
}
公共异步任务索引()
{
List documentTypeliste=新列表();
使用(var httpClient=new httpClient())
{
使用(var response=wait-httpClient.GetAsync(“https://localhost:44318/api/DocumentType"))
{
var apiResponse=await response.Content.ReadAsStringAsync();
documentTypeliste=JsonConvert.DeserializeObject(apiResponse);
}
}
返回视图(documentTypeliste);
}
但是,我遇到了一个错误,低于该错误,我仍然无法解决。谢谢你的帮助
JsonSerializationException:无法反序列化当前JSON对象
(例如,{“名称”:“值”})转换为类型
'System.Collections.Generic.List'1[GEDRH.Models.Entities.DocumentType]'
因为该类型需要一个JSON数组(例如[1,2,3])来反序列化
正确地要修复此错误,请将JSON更改为JSON数组
(例如[1,2,3])或更改反序列化类型,使其成为正常类型
.NET类型(例如,不是integer之类的基元类型,也不是集合
可以从JSON反序列化的类型(如数组或列表)
对象还可以将JsonObjectAttribute添加到类型以强制它
从JSON对象反序列化。路径“成功”,第1行,位置
十一,
Yiyi You,Pranav Hosangadi我的回答:
{"success":true,"message":"Documents type trouve","data":[{"id":1,"name":"Type 1","description":"Description du type 1"},{"id":2,"name":"Type 2","description":"Description du type 2"},{"id":3,"name":"Type 3","description":"Description du type 3"},{"id":4,"name":"Type 4","description":"Description du type 4"},{"id":5,"name":"Type 5","description":"Description du type 5"},{"id":6,"name":"Type 6","description":"Description du type 6"}]}
我解决了一个问题
public async Task<IActionResult> Index()
{
var documentTypeliste = new List<DocumentType>();
using (var httpClient = new HttpClient())
{
using (var response = await httpClient.GetAsync("https://localhost:44318/api/DocumentType"))
{
var apiResponse = await response.Content.ReadAsStringAsync();
var list = JObject.Parse(apiResponse)["data"].Select(el => new { Id = (int)el["id"], Name = (string)el["name"], Description = (string)el["description"] }).ToList();
//var names = list.Select(p => p.Name).ToList();
//var descriptions = list.Select(p => p.Description).ToList();
//var ids = list.Select(p => p.Id).ToList();
foreach(var it in list)
{
DocumentType dt = new DocumentType
{
Id = it.Id,
Name = it.Name,
Description = it.Description
};
documentTypeliste.Add(dt);
}
}
}
ViewBag.PersonTypeListe = _servicePersonType.SelectAll();
return View(documentTypeliste);
}
公共异步任务索引()
{
var documentTypeliste=新列表();
使用(var httpClient=new httpClient())
{
使用(var response=wait-httpClient.GetAsync(“https://localhost:44318/api/DocumentType"))
{
var apiResponse=await response.Content.ReadAsStringAsync();
var list=JObject.Parse(apiResponse)[“data”].Select(el=>new{Id=(int)el[“Id”],Name=(string)el[“Name”],Description=(string)el[“Description”}).ToList();
//var Name=list.Select(p=>p.Name.ToList();
//var descriptions=list.Select(p=>p.Description.ToList();
//var Id=list.Select(p=>p.Id).ToList();
foreach(在列表中对其进行var)
{
DocumentType dt=新的DocumentType
{
Id=it.Id,
Name=it.Name,
Description=it.Description
};
documentTypeliste.Add(dt);
}
}
}
ViewBag.PersonTypeListe=\u servicePersonType.SelectAll();
返回视图(documentTypeliste);
}
检查web应用程序返回的json响应。错误告诉您,它在顶层没有列表,因此您无法将其反序列化为列表
。您可以共享您的apiResponse吗?正如Pranav Hosangadi
所说,它无法反序列化为列表。我刚刚共享了我的apiResponse