Instead of doing
MyRecordDetails details = JsonSerializer.Deserialize<MyRecordDetails>(dtls, options);
try to do
var details = JsonSerializer.Deserialize<RecordDetail[]>(dtls, options);
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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.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; }
}
Instead of doing
MyRecordDetails details = JsonSerializer.Deserialize<MyRecordDetails>(dtls, options);
try to do
var details = JsonSerializer.Deserialize<RecordDetail[]>(dtls, options);
var details = JsonSerializer.Deserialize<MyRecordDetails[]>(dtls, options);
returns details with 3 elements, each one is null
Do not use MyRecordDetails[] but instead RecordDetail[] only.
Some progress....
each element in details now has what is shown in the screen clip, however the data I am looking for is null: