Blazor WASM "Cannot deserialize the current JSON object (e.g. {"name":"value"})" 错误

Blazor WASM "Cannot deserialize the current JSON object (e.g. {"name":"value"})" error

我一直在测试 Blazor WebAssembly 以及能够 POST 到 API(带有 JSON)正文内容,检索正文内容并将其放入的能力到页面内使用的变量。 我遇到了一个问题,导致我返回以下错误:

未处理的异常呈现组件:无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型 'WebApplication4.Pages.FetchData+parentArenas[]',因为该类型需要 JSON 数组(例如 [1,2,3])以正确反序列化。 要修复此错误,请将 JSON 更改为 JSON 数组(例如 [1,2,3])或更改反序列化类型,使其成为普通的 .NET 类型(例如,不是原始类型像整数,而不是像数组或列表这样的集合类型),可以从 JSON 对象反序列化。也可以将 JsonObjectAttribute 添加到类型以强制它从 JSON 对象反序列化。

这是我的 类:

    public class ArenaDetail
    {
        public string ArenaName { get; set; }
        public string ArenaNumber { get; set; }
        public string ArenaDescription { get; set; }
        public string ArenaAddress { get; set; }
        public string ArenaType { get; set; }
        public string ArenaGUID { get; set; }
    }

    public class parentArenas
    {
        public List<ArenaDetail> ArenaDetails { get; set; }
    }

这是用于 post 到 API 并检索响应的代码:

       
private parentArenas[] arenas;


protected override async Task OnInitializedAsync()
 {
              
        var source = new CancellationTokenSource();
        var httpClient = new HttpClient();
        var myguid= new Centre { myguid= "CA8E99BC-F5B0-4A54-A8BF-C9AE9E95EE44" };
        var response = await httpClient.PostAsJsonAsync("https://api.azurewebsites.net/api/GetAllArenas?code=Nve2P5GcqexzAvAnvOYqBySLT6SuRDQpBHA==", myguid, source.Token);
        string stringresponse = await response.Content.ReadAsStringAsync();
        arenas = JsonConvert.DeserializeObject<parentArenas[]>(stringresponse);

}

最后一行抛出上述错误。

如果我在我的代码中放置一个断点,我可以看到 stringresponse 包含返回的数据:

'{\r\n  "ArenaDetails": [\r\n    {\r\n      "ArenaName": "Court1",\r\n      "ArenaNumber": "1",\r\n      "ArenaDescription": "court 1",\r\n      "ArenaAddress": "1 A Street",\r\n      "ArenaType": "hardcourt",\r\n      "ArenaGUID": "21FD0044-0B6F-4650-AD36-1E280FF70C8B"\r\n    },\r\n    {\r\n      "ArenaName": "Court2",\r\n      "ArenaNumber": "2",\r\n      "ArenaDescription": "court 2",\r\n      "ArenaAddress": "",\r\n      "ArenaType": "hardcourt",\r\n      "ArenaGUID": "D7E491D5-C300-40C3-8378-723FE8F3539D"\r\n    },\r\n    {\r\n      "ArenaName": "Court3",\r\n      "ArenaNumber": "3",\r\n      "ArenaDescription": "court 3",\r\n      "ArenaAddress": "",\r\n      "ArenaType": "hardcourt",\r\n      "ArenaGUID": "C669ABA0-5BAC-4284-8C10-CE9GDA45A3D1"\r\n    },\r\n    {\r\n      "ArenaName": "Show Court 3",\r\n      "ArenaNumber": "",\r\n      "ArenaDescription": "Show court 3",\r\n      "ArenaAddress": "",\r\n      "ArenaType": "",\r\n      "ArenaGUID": "C669ABA0-5BAC-4284-8C00-CE97DA45A3D1"\r\n    }\r\n  ]\r\n}'

我从 API 得到的响应如下(使用 Postman 看到的)

{
    "ArenaDetails": [
        {
            "ArenaName": "Court1",
            "ArenaNumber": "1",
            "ArenaDescription": "court 1",
            "ArenaAddress": "1 A Street",
            "ArenaType": "hardcourt",
            "ArenaGUID": "21FD0044-0B6F-4650-AD36-1E280FF70C8B"
        },
        {
            "ArenaName": "Court2",
            "ArenaNumber": "2",
            "ArenaDescription": "court 2",
            "ArenaAddress": "",
            "ArenaType": "hardcourt",
            "ArenaGUID": "D7E491D5-C300-40C3-8378-723FE8F3539D"
        },
        {
            "ArenaName": "Court3",
            "ArenaNumber": "3",
            "ArenaDescription": "court 3",
            "ArenaAddress": "",
            "ArenaType": "hardcourt",
            "ArenaGUID": "C669ABA0-5BAC-4284-8C10-CE9GDA45A3D1"
        }
    ]
}

根据我读过的与同一问题相关的其他一些文章,我尝试更改

 arenas = JsonConvert.DeserializeObject<parentArenas[]>(stringresponse);

var myarenas = JsonConvert.DeserializeObject<List<parentArenas[]>>(stringresponse);

然而,这会产生同样的错误。最终,我希望能够用内容填充我的“arenas”变量,以便我可以在我的 Razor 页面语法中使用它

希望我的问题有意义......

知道如何解决这个问题吗?无论如何,如果有更好的方法来实现这一目标,请告诉我。感谢任何帮助 - 有一段时间我一直在用头撞墙。

您不需要使用 [] 或列表

var result = JsonConvert.DeserializeObject<parentArenas>(stringresponse);

List<ArenaDetail> arenas=result.ArenaDetails;