Blazor Server - deserializing an array of objects with System.Text.Json - The JSON value could not be found

wavemaster 311 Reputation points
2021-03-07T14:25:24.41+00:00

The JSON value could not be converted to BtServer.Pages.RecordDetail+MyRecordDetails. Path: $ | LineNumber: 0 | BytePositionInLine: 1.

at System.Text.Json.ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(Type propertyType)
at System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value) at System.Text.Json.Serialization.JsonConverter1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
at System.Text.Json.JsonSerializer.ReadCore[TValue](Utf8JsonReader& reader, Type returnType, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, Type returnType, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options)
at BtServer.Pages.RecordDetail.<OnInitializedAsync>d__12.MoveNext() in C:\Users\Robert\owner\Repos\BtApiCore5\BtServer\Pages\RecordDetail.razor:line 92

public class MyRecordDetails
    {
        public IEnumerable<RecordDetail> details { get; set; }   

        //tried this one too! same error message: public List<RecordDetail> details { get; set; } 
    }


    protected override async Task OnInitializedAsync()
    {
        try
        {
            var httpClient = _clientFactory.CreateClient("ServerAPI");
            httpClient.DefaultRequestHeaders.Add("Accept", "application/json");

            var json = JsonSerializer.Serialize(recordDetails);
            var content = new StringContent(json);

            content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");

            var options = new JsonSerializerOptions()
            {
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase
            };

            HttpResponseMessage response = await httpClient.GetAsync($"{baseUrl}/api/Record/DetailByClient/22/49/3");


            string **dtls** = await response.Content.ReadAsStringAsync();

            if (dtls is not null)
            {
                MyRecordDetails details = JsonSerializer.Deserialize<MyRecordDetails>(dtls, options);
            }
            else
            {
                //do something    
            }

        }
        catch (Exception ex) { Console.WriteLine(ex.Message); }                
    }

dtls holds:

[
{"TransactionId":1015,"CName":"Don","TDate":"May 9 2020 4:54PM","BDate":"May 9 2020 4:54PM","Initials":"JMC","IsBilled":false,"IsPaid":false,"SvcLevel":"R1"},
{"TransactionId":988,"CName":"Don","TDate":"Jan 23 2020 3:13PM","BDate":"Mar 10 2020 11:17AM","Initials":"JMC","IsBilled":false,"IsPaid":false,"SvcLevel":"R1"},
{"TransactionId":974,"CName":"Don","TDate":"Jan 22 2020 2:36PM","BDate":"Jan 22 2020 2:36PM","Initials":"JMC","IsBilled":false,"IsPaid":false,"SvcLevel":"R1"}
]

What am I doing wrong here?

Oh, and here is RecordGrid.cs

public class RecordDetail
{
public int TransactionId { get; set; }
public string CName { get; set; }
public string TDate { get; set; }
public string BDate { get; set; }
public string Initials { get; set; }
public bool IsBilled { get; set; }
public bool IsPaid { get; set; }
public string SvcLevel { get; set; }

    //public List<ServiceItem> ServiceItems { get; set; }

}
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,397 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,499 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Mattias Asplund 236 Reputation points
    2021-03-07T15:28:00.91+00:00

    Instead of doing

    MyRecordDetails details = JsonSerializer.Deserialize<MyRecordDetails>(dtls, options);
    

    try to do

    var details = JsonSerializer.Deserialize<RecordDetail[]>(dtls, options);
    
    0 comments No comments

  2. wavemaster 311 Reputation points
    2021-03-07T15:50:31.507+00:00

    var details = JsonSerializer.Deserialize<MyRecordDetails[]>(dtls, options);

    returns details with 3 elements, each one is null

    0 comments No comments

  3. Mattias Asplund 236 Reputation points
    2021-03-07T16:00:57.52+00:00

    Do not use MyRecordDetails[] but instead RecordDetail[] only.

    0 comments No comments

  4. wavemaster 311 Reputation points
    2021-03-07T16:13:11.99+00:00

    Some progress....

    each element in details now has what is shown in the screen clip, however the data I am looking for is null:

    75059-image.png

    0 comments No comments