Home > other >  Blazor WASM "Cannot deserialize the current JSON object (e.g. {"name":"value&quo
Blazor WASM "Cannot deserialize the current JSON object (e.g. {"name":"value&quo

Time:11-17

I've been testing out Blazor WebAssembly and the ability to be able to POST to an API (with JSON) body content, retrieve the body content and put it into a variable for use within the page. I'm having an issue whereby I get returned the following error:

Unhandled exception rendering component: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'WebApplication4.Pages.FetchData parentArenas[]' 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) 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.

Here are my classes:

    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; }
    }

Here is the code use to post to the API and retrieve the response:

       
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);

}

This final line is throwing the error noted above.

If I place a breakpoint in my code I can see that the stringresponse contains the returned data:

'{\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}'

The response I'm getting from the API is as follows (as seen using 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"
        }
    ]
}

As per some other articles I've read relating to the same problem I've tried changing

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

to

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

However this produces the same error. Ultimately I want to be able to populate my "arenas" variable with the content so I can use it within my Razor page syntax

Hope my issue makes sense….

Any idea how I can get around this issue please? By all means if there is a better approach to achieving this then please let me know. Any help is appreciated - been banging my head against a wall on this for a while.

CodePudding user response:

you don' t need to use [] or list

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

List<ArenaDetail> arenas=result.ArenaDetails;
  • Related