我正在嘗試從 API (https://localhost:7270/api/GetEmployees) 獲取資料。從 API 獲取所有必需的資料后,我只想反序列化為 C# 物件并需要在 HTML 頁面中顯示。
這是 JSON 資料
"{\"data\":[{\"id\":1,\"firstName\":\"Sandanuwan\",\"lastName\":\"Dharmarathna\",\"address\":\"Kurunegala\",\"dob\":\"2022-08-23T22:20:00\",\"age\":26,\"class\":\"10 B\",\"emailAddress\":\"[email protected]\",\"department\":{\"id\":2,\"departmentName\":\"HR\"}},{\"id\":2,\"firstName\":\"Anna\",\"lastName\":\"Merry\",\"address\":\"London\",\"dob\":\"2022-04-20T00:00:00\",\"age\":23,\"class\":\"7 A\",\"emailAddress\":\"[email protected]\",\"department\":{\"id\":1,\"departmentName\":\"Account\"}}],\"success\":true,\"message\":\"Successfully received all Employees Details\"}"
為了反序列化上面的 JSON 資料,我創建了如下所示的 C# 類。
員工類
public class Employee
{
public int Id { get; set; }
public string FirstName { get; set; } = string.Empty;
public string LastName { get; set; } = string.Empty;
public string Address { get; set; } = string.Empty;
public DateTime DOB { get; set; }
public int Age { get; set; }
public string Class { get; set; } = string.Empty;
public string EmailAddress { get; set; } = string.Empty;
public Department Department { get; set; }
}
部門班
public class Department
{
public int Id { get; set; }
public string DepartmentName { get; set; } = string.Empty;
}
根類
public class Root
{
public List<Employee> Data { get; set; }
public bool Success { get; set; }
public string Message { get; set; }
}
控制器索引法
public async Task<IActionResult> Index()
{
List<Root> employeeList = new List<Root>();
using (var httpClient = new HttpClient())
{
using (var response = await httpClient.GetAsync("https://localhost:7270/api/GetEmployees"))
{
string apiResponse = await response.Content.ReadAsStringAsync();
employeeList = JsonConvert.DeserializeObject<List<Root>>(apiResponse);
}
}
return View(employeeList);
}
索引 Html 頁面
<table >
<thead>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Date Of Birth</th>
<th>Age</th>
<th>Class</th>
<th>Email Address</th>
<th>Department</th>
</tr>
</thead>
<tbody>
@foreach (var r in Model)
{
<tr>
<td>@r.Data[0].Id</td>
<td>@r.Data[1].FirstName</td>
</tr>
}
</tbody>
</table>
但是在運行 Web 應用程式后,我收到以下錯誤。
An unhandled exception occurred while processing the request.
JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[EmployeeWeb.Models.Root]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'data', line 1, position 8.
據我了解,錯誤來自這條線
employeeList = JsonConvert.DeserializeObject<List<Root>>(apiResponse);
所以,有人可以幫我解決這個問題。謝謝
uj5u.com熱心網友回復:
將 apiResponse 反序列化為Root,而不是List<Root>:
List<Employee> employeeList = new List<Employee>();
...
var root = JsonConvert.DeserializeObject<Root>(apiResponse);
employeeList = root.Data;
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/516911.html
標籤:asp.net 核心asp.net-web-apiasp.net-web-api2
上一篇:如何將輸入元素設定為禁用
