我正在尝试使用以下调用从Blazor服务器应用程序调用Rest API以获取项目列表。API返回正确,但无法映射到项目对象,因为项目的JSON嵌套在结果属性下。
这是我打的电话
var projects = await httpClient.GetJsonAsync<Project[]>("projects");
这就是API调用的响应。
{
"message": "GET Request successful.",
"isError": false,
"result": [
{
"projectCode": "PRJ-1996",
"businessAssociateID": "BA000000000002",
"contractStartDate": "11/19/2020 8:37:17 AM",
"contractEndDate": "11/19/2020 8:37:17 AM",
"contractStatus": "Active",
"createdBy": "system",
"createdDate": "2020-11-19T08:37:17.37",
"active": true
},
{
"projectCode": "PRJ-1997",
"businessAssociateID": "BA000000000002",
"contractStartDate": "11/19/2020 8:37:17 AM",
"contractEndDate": "11/19/2020 8:37:17 AM",
"contractStatus": "Active",
"createdBy": "system",
"createdDate": "2020-11-19T08:37:17.37",
"active": true
},
{
"projectCode": "PRJ-1998",
"businessAssociateID": "BA000000000002",
"contractStartDate": "11/19/2020 8:37:17 AM",
"contractEndDate": "11/19/2020 8:37:17 AM",
"contractStatus": "Active",
"createdBy": "system",
"createdDate": "2020-11-19T08:37:17.37",
"active": true
},
{
]
}
如何使用GetJsonAsync函数映射到Projects列表。我看到的所有示例都只返回对象数组,而不返回响应消息。
这是我在电话中收到的错误。路由对于API调用是正确的,并且已经测试过。
JsonException: The JSON value could not be converted to Web.Data.Project[]. Path: $ | LineNumber: 0 | BytePositionInLine: 1.
是一种快速而简单的解决方案
class Response
{
public string Message { get; set; }
public bool IsError { get; set; }
public Project[] Result { get; set; }
}
这也让您可以查看IsError和Message。