I want to parse JSON dynamic KeyValuePair response to C# model but getting an error. I am building an application using Blazor webassembly (latest version). I am quite sure that I am doing a silly mistake in JSON deserialization. Can you please correct me.
I am getting below JSON response from API:
{
"message": "Success",
"data": {
"stores": {
"": "Select Store",
"21": "Indore store",
"33": "test POS2",
"34": "test POS4",
"37": "test store 11",
"36": "test store09",
"59": "test store26"
},
"storeId": "21",
"businessId": "1"
}
}
And trying to parse response like this:
public async Task GetSetFilters()
{
SetApiHeaders();
var response = await Http.GetAsync("/pwa/v1/transaction-filters");
TransactionFiltersModel content = await response.Content.ReadFromJsonAsync<TransactionFiltersModel>();
}
My TransactionFiltersModel is:
public class TransactionFiltersModel
{
public string message { get; set; }
public TransactionFiltersData data { get; set; }
}
public class TransactionFiltersData
{
public string storeId { get; set; }
public int businessId { get; set; }
public List<KeyValuePair<string, string>> stores { get; set; }
}
Getting error:
Unhandled exception rendering component: The JSON value could not be converted to System.Collections.Generic.List
1[System.Collections.Generic.KeyValuePair
2[System.String,System.String]]. Path: $.data.stores | LineNumber: 1 | BytePositionInLine: 43. System.Text.Json.JsonException: The JSON value could not be converted to System.Collections.Generic.List1[System.Collections.Generic.KeyValuePair
2[System.String,System.String]]. Path: $.data.stores | LineNumber: 1 | BytePositionInLine: 43. at System.Text.Json.ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue (System.Type propertyType) <0x3070550 + 0x00032> in :0 .....