c# - 如何在 Blazor WASM 中将 JSON 动态 KeyValuePair 解析为 C# 模型
问题描述
我想解析对 C# 模型的 JSON 动态 KeyValuePair 响应,但出现错误。我正在使用 Blazor webassembly(最新版本)构建应用程序。我很确定我在 JSON 反序列化中犯了一个愚蠢的错误。你能纠正我吗?
我从 API 得到以下 JSON 响应:
{
"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"
}
}
并尝试像这样解析响应:
public async Task GetSetFilters()
{
SetApiHeaders();
var response = await Http.GetAsync("/pwa/v1/transaction-filters");
TransactionFiltersModel content = await response.Content.ReadFromJsonAsync<TransactionFiltersModel>();
}
我的 TransactionFiltersModel 是:
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; }
}
得到错误:
未处理的异常呈现组件:无法将 JSON 值转换为 System.Collections.Generic.List
1[System.Collections.Generic.KeyValuePair
2[System.String,System.String]]。路径:$.data.stores | 行号:1 | BytePositionInLine:43。 System.Text.Json.JsonException:JSON 值无法转换为 System.Collections.Generic.List1[System.Collections.Generic.KeyValuePair
2[System.String,System.String]]。路径:$.data.stores | 行号:1 | BytePositionInLine: 43. 在 System.Text.Json.ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue (System.Type propertyType) <0x3070550 + 0x00032> in :0 .....
解决方案
推荐阅读
- google-maps - Google Api 未提供位置
- elasticsearch - 未索引的 ElasticSearch 字段(零)上的 Grafana 过滤器,因此它们不会显示在表格面板中
- aframe - 如何在 A-frame webvr 中 6 分钟后生成对象?
- c# - 如何为文件名添加第二个扩展名?
- mapkit - Mapkit.js 反向地理编码不给城市
- r - 用零替换 NA - bmerge 中的错误
- powershell - DBATOOLS - 从表中复制数据并在 DestinationDatabase 上创建
- c# - 提供者没有返回 ProviderManifestToken 字符串 - 无法连接到 MySql 主机
- r - 将函数映射到研究的每个参与者
- ios - 使用 swift 使用 exif 数据将图像保存到 iOS 中的照片库