API 结果未按预期反序列化

问题描述 投票:0回答:2

我正在构建一个与 .NET 5 API 通信的 blazor Web 应用程序。

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

课程如下:

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:JSON 值无法转换为 System.Collections.Generic.List

处理此数据以反序列化数据以便我可以将其传递到我的 Blazor 组件的正确方法是什么?我是否需要创建一个将单个结果属性定义为列表的视图模型,或者我是否在 API/Blazor 应用程序中缺少一些可以使其正常工作的内容!

c# asp.net-core blazor-server-side .net-5
2个回答
0
投票

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

我可能会尝试这样的事情:

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

然后拆箱

(await JsonSerializer.DeserializeAsync<result>).locations

0
投票

我设法弄清了问题的根源,问题出在 API 级别,一旦我更改了方法返回类型,我就返回了 IAction 结果而不是任务,一切都开始工作了!

旧:

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

新:

[HttpGet]
        public Task<IActionResult> GetAllLocations()
        {
            return Ok(_locationManagementRepository.GetAllLoacations());
        }
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.