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

wavemaster 311 Reputation points
2021-03-05T16:23:36.017+00:00

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?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,164 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,389 questions
0 comments No comments
{count} votes

Accepted answer
  1. Michael Wang-MSFT 1,051 Reputation points
    2021-03-08T07:59:41.043+00:00

    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

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. wavemaster 311 Reputation points
    2021-03-09T03:50:11.957+00:00

    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!

    1 person found this answer helpful.
    0 comments No comments