question

wavemaster avatar image
0 Votes"
wavemaster asked ·

System.Text.Json deserialization challenges in a Blazor Server app

I am having this problem with not getting my data as is described in detail here: https://www.pmichaels.net/2020/02/22/my-object-wont-deserialise-using-system-text-json/

using System.Text.Json
using System.Text.Json.Serialization;

private List<RecordDetail> recordDetails;

var httpClient = _clientFactory.CreateClient("ServerAPI");
httpClient.DefaultRequestHeaders.Add("Accept", "application/json");

         var json = JsonSerializer.Serialize(recordDetails);
         var content = new StringContent(json);
         string errorMessage = null;
         content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");

         HttpResponseMessage response = await httpClient.GetAsync($"{baseUrl}/api/Record/DetailByClient/22/49/3");
         string dtls = await response.Content.ReadAsStringAsync();
                  
         if (dtls is not null)
         {
             var **recordDetails** = JsonSerializer.Deserialize(dtls, typeof(List<RecordDetail>)) as List<RecordDetail>;
         }

dtls looks like this:
[{"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":"D n","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"}]

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

74865-image.png



Without the options added to Deserialize I am seeing the exact same thing as described in the post I linked to.

What am I doing wrong here?


Then, with a different notation var recordDetails = JsonSerializer.Deserialize<RecordDetail>(dtls, options);

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


Same question, what am I doing wrong here?

dotnet-aspnetcore-blazordotnet-aspnetcore-webapi
image.png (69.7 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.

miwan2-msft avatar image
0 Votes"
miwan2-msft answered ·

Hi, @MyLadys-4900 ,

I tried both System.Text.Json and Newtonsoft.Json. And it works as expect. It might be related with the format of your dtls. You could try to deserialize by Newtonsoft.Json.
Newtonsoft.Json is flexible by default while System.Text.Json is strict.

Below are codes.

 var dtls = "[{\"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\"}]";

         List<RecordDetail> details = System.Text.Json.JsonSerializer.Deserialize<List<RecordDetail>>(dtls);
         foreach (RecordDetail record in details)
         {
             Console.WriteLine("TransactionId: " + record.TransactionId);
             Console.WriteLine("CName: " + record.CName);
             Console.WriteLine("TDate: " + record.TDate);
             Console.WriteLine("BDate: " + record.BDate);
             Console.WriteLine("Initials: " + record.Initials);
             Console.WriteLine("IsBilled: " + record.IsBilled);
             Console.WriteLine("IsPaid: " + record.IsPaid);
             Console.WriteLine("SvcLevel: " + record.SvcLevel);
             Console.WriteLine("");
         }

         List<RecordDetail> ndetails = Newtonsoft.Json.JsonConvert.DeserializeObject<List<RecordDetail>>(dtls);
         foreach (RecordDetail record in ndetails)
         {
             Console.WriteLine("TransactionId: " + record.TransactionId);
             Console.WriteLine("CName: " + record.CName);
             Console.WriteLine("TDate: " + record.TDate);
             Console.WriteLine("BDate: " + record.BDate);
             Console.WriteLine("Initials: " + record.Initials);
             Console.WriteLine("IsBilled: " + record.IsBilled);
             Console.WriteLine("IsPaid: " + record.IsPaid);
             Console.WriteLine("SvcLevel: " + record.SvcLevel);
             Console.WriteLine("");
         }
     }

Screenshot

75300-image.png


If the answer doesn’t solve your issue, please provide more details of error that will help us track down what’s happening.
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Best Regards,
Michael Wang





image.png (35.0 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.

wavemaster avatar image
1 Vote"
wavemaster answered ·

Ok, figured it out.

I have a "view model" called RecordDetail.cs, you have seen it in this thread, it lives in the Web API project.

I also have a RecordDetail.razor page, it lives in the Blazor Server project




When you declare this:

private List<RecordDetail> recorddetails;

It is making  List<RecordDetails.blazor> apparently.  That fails silently, there are no warnings about ambiguity.

Too many leaves up in the air, and focused on the wrong leaf (deserialization).

Thanks for eliminating the serialization leaves!

·
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.