API 结果未按预期反序列化

API result not deserializing as expected

我正在构建一个与 .NET 5 通信的 blazor 网络应用 API。

我收到反序列化错误,我认为原因是因为返回的对象与我定义的对象不完全相同。

class如下:

public class Location
{
    [Key]
    public Guid Id { get; set; }

    [Required]
    public Guid WorkspaceID { get; set; }

    [Required]
    public string Name { get; set; }

    public string ImagePath { get; set; }

    public ICollection<Floor> Floors { get; set; }
}

API:

 [HttpGet]
        public IActionResult GetAllLocations()
        {
            return Ok(_locationManagementRepository.GetAllLoacations());
        }

数据访问方法:

public async Task<IEnumerable<Location>> GetAllLoacations()
        {
            var locations = new [] {
                new Location{Id = Guid.NewGuid(), Name = "Location 1", WorkspaceID = Guid.NewGuid(),Floors = null, ImagePath = null},
                new Location{Id = Guid.NewGuid(), Name = "Location 2", WorkspaceID = Guid.NewGuid(),Floors = null, ImagePath = null},
                new Location{Id = Guid.NewGuid(), Name = "Location 3", WorkspaceID = Guid.NewGuid(),Floors = null, ImagePath = null}
            };
            return await Task.FromResult(locations);
        }

查询API的Blazor服务:

 public async Task<IEnumerable<Location>> GetAllLocations()
        {
            _httpClient.SetBearerToken(await _tokenManager.RetrieveAccessTokenAsync());

            return await JsonSerializer.DeserializeAsync<List<Location>>
                (await _httpClient.GetStreamAsync($"api/location"),
                new JsonSerializerOptions() { PropertyNameCaseInsensitive = true });
        }

本方法返回的JSON如下:

{"result":[{"id":"e029e1cb-3ea2-4b2c-acae-5e693d2ece46","workspaceID":"985f2695-f36e-4481-9b7c-b1e00bbb4ac3","name":"Location 1","imagePath":null,"floors":null},{"id":"c4ccb92b-9cc8-4e08-bba3-22f61c1fe7e5","workspaceID":"67d05f55-5bd4-4e8f-93b3-b3daa9155535","name":"Location 2","imagePath":null,"floors":null},{"id":"a42073da-239d-428e-a318-d3dc7cfd221b","workspaceID":"d3a14500-ddcd-486f-a96e-5c86373c092d","name":"Location 3","imagePath":null,"floors":null}],"id":2,"exception":null,"status":5,"isCanceled":false,"isCompleted":true,"isCompletedSuccessfully":true,"creationOptions":0,"asyncState":null,"isFaulted":false}

我得到的错误是:

JsonException: The JSON value could not be converted to System.Collections.Generic.List

我的问题是,处理此数据以反序列化数据以便将其传递到我的 blazor 组件的正确方法是什么?我是否需要创建一个将单个结果 属性 定义为 List 的视图模型,或者我是否在我的 API/Blazor 应用程序中遗漏了一些可以使它正常工作的东西! :-)

预先感谢您的帮助。

我不是 JSON 专家,如果“结果”是 JSON API 响应的标准领导者,请原谅我,但我的猜测是它正在尝试将“结果”对象解压为数组。

我可能会尝试类似的方法:

public class result {
   List<Location> locations{get; set;}
}

然后开箱

(await JsonSerializer.DeserializeAsync<result>).locations

我设法追根究底,问题出在 API 级别,一旦我更改了方法 return 我正在 return 使用 IAction 结果而不是任务 return 输入一切开始工作!

旧:

[HttpGet]
        public IActionResult GetAllLocations()
        {
            return Ok(_locationManagementRepository.GetAllLoacations());
        }

新:

[HttpGet]
        public Task<IActionResult> GetAllLocations()
        {
            return Ok(_locationManagementRepository.GetAllLoacations());
        }