从.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数据中的名称完全相同)

有什么想法吗?

wanyi20060700 回答:从.Net Core 3.0中的API反序列化JSON流,使所有字段都为空

您应该使用[JsonPropertyName(“”)]标记每个属性,因为json区分大小写(我认为)。

本文链接:https://www.f2er.com/3153181.html

大家都在问