json - 从 .Net Core 3.0 中的 API 反序列化 JSON 流,使所有字段为空
问题描述
我有一个简单的 WPF / .Net Core 3.0 应用程序,它在 Web API 端点上执行 GET:
private HttpClient httpClient = new HttpClient();
private async Task GetClients()
{
var serializer = new DataContractJsonSerializer(typeof(List<ClientGetDto>));
var streamTask = httpClient.GetStreamAsync("https://mywebapp.com/api/Clients");
List<ClientGetDto> clientDtos = serializer.ReadObject(await streamTask) as List<ClientGetDto>;
}
ClientGetDto 看起来像这样:
{
public int Id { get; set; }
public string ClientCode { get; set; }
public string ApiUrl { get; set; }
public string CompanyName { get; set; }
public string FranchiseName { get; set; }
public int? ProLicenses { get; set; }
public int? LiteLicenses { get; set; }
public int? ProSalesLicenses { get; set; }
public int? LiteSalesLicenses { get; set; }
public bool? IsActive { get; set; }
public DateTime? StartOfAgreementDate { get; set; }
public int? DebitOrderDay { get; set; }
public DateTime? DebitOrderStartDate { get; set; }
public decimal? ContractAmount { get; set; }
public bool? DebitOrderFormReceived { get; set; }
public bool? CancellationReceived { get; set; }
public DateTime? CancellationDate { get; set; }
public string CompanyRegNo { get; set; }
public string DbUrl { get; set; }
public string DbName { get; set; }
public double? CloudStorageQuota { get; set; }
public string Comments { get; set; }
public int? FranchiseId { get; set; }
public bool? IsTestDb { get; set; }
public bool? IsGumtreeRegistered { get; set; }
public int? FusionClientId { get; set; }
public string CountryCode { get; set; }
}
API 返回的 JSON 是:
[
{
"id": 3,
"clientCode": "cx0007",
"apiUrl": "https://mywebapp/api",
"companyName": "ACME Company",
"franchiseName": "ACME Franchise",
"proLicenses": 1,
"liteLicenses": 0,
"proSalesLicenses": 0,
"liteSalesLicenses": 0,
"isActive": true,
"startOfAgreementDate": "2007-08-01T00:00:00",
"debitOrderDay": 1,
"debitOrderStartDate": "2012-03-01T00:00:00",
"contractAmount": 695.00,
"debitOrderFormReceived": true,
"cancellationReceived": false,
"cancellationDate": "2012-10-18T00:00:00",
"companyRegNo": "",
"dbUrl": "mydb.co.za",
"dbName": "db1",
"cloudStorageQuota": 5.0,
"comments": null,
"franchiseId": null,
"isTestDb": false,
"isGumtreeRegistered": false,
"fusionClientId": null,
"countryCode": "US"
},
...
]
我的问题是代码正确地将 JSON 反序列化为 ClientGetDto 对象列表,但所有字段均为空。它不会抛出任何异常或任何东西。我试过用 [DataContract] 和 [DataMember] 装饰我的 ClientGetDto 模型,但没有区别(也不会,因为模型的字段名称与 JSON 数据中的字段名称完全相同)
有任何想法吗?
解决方案
您应该用 [JsonPropertyName("")] 标记每个属性,因为 json 区分大小写(我认为)。
推荐阅读
- java - Quartz 作业注解@DisallowConcurrentExecution 实现
- build - 在跨平台环境中管理 conda env
- php - php.ini 已修改,但无法上传文件
- docker - Magento 2中的Scandipwa-base_ssl-term错误
- python - 位置论据据说是缺失的,但给出了,有什么问题?
- laravel - 表单关闭时自动打印
- linux - 如何将静态网站副本的 URL 缩短为少于 256 个字符?(使用 wget 创建)
- node.js - 摩卡前功能无法正常工作
- rabbitmq - 对 RabbitMQ 服务器上的所有交换进行窃听
- node.js - Firebase Cloud 函数返回 2 个集合的数据