question

wavemaster avatar image
0 Votes"
wavemaster asked ·

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

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.ObjectDefaultConverter`1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
at System.Text.Json.Serialization.JsonConverter`1.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; }
        
 }
dotnet-aspnetcore-blazordotnet-aspnetcore-webapi
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MattiasAsplund-3483 avatar image
0 Votes"
MattiasAsplund-3483 answered ·

Instead of doing

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

try to do

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



·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

wavemaster avatar image
0 Votes"
wavemaster answered ·

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

returns details with 3 elements, each one is null

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MattiasAsplund-3483 avatar image
0 Votes"
MattiasAsplund-3483 answered ·

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

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

wavemaster avatar image
0 Votes"
wavemaster answered ·

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



image.png (31.6 KiB)
·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.