在.Net Core 3.0中反序列化DateTime JSON字段时出错



我有一个WPF/.Net Core 3.0应用程序,它正在使用Web API。它在API端点上执行GET,然后尝试反序列化JSON。但是,在尝试反序列化DateTime字段时,它会出现错误。这是代码:

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"
},
...
]

我得到的错误是:

"反序列化类型为的对象时出错System.Collections.Generic.List`1[[PropWorxManager.DTOs.ClientGetDto,PropWorxManager,版本=1.0.0.0,区域性=中性,PublicKeyToken=null]]。DateTime内容"2007-08-01T00:00:00"没有按要求以'\/Date('开头,以'\/'结尾JSON。"}System.Runtime.Serialization.Serialization异常

经过一些研究,我尝试了这个:

var settings = new DataContractJsonSerializerSettings
{
DateTimeFormat = new System.Runtime.Serialization.DateTimeFormat("o")
};
var serializer = new DataContractJsonSerializer(typeof(List<ClientGetDto>), settings);

但这就给出了这个错误:

"反序列化类型为的对象时出错System.Collections.Generic.List`1[[PropWorxManager.DTOs.ClientGetDto,PropWorxManager,版本=1.0.0.0,区域性=中性,PublicKeyToken=null]]。无法识别字符串"2007-08-01T00:00:00"作为有效日期时间。"}System.Runtime.Serialization.Serialization异常

欢迎提出任何建议。非常感谢。

p.S.API也是用.Net Core 3.0编写的,如果这有帮助的话。。。

这很有效:

var settings = new DataContractJsonSerializerSettings
{
DateTimeFormat = new System.Runtime.Serialization.DateTimeFormat("o")
};
var serializer = new DataContractJsonSerializer(typeof(List<ClientGetDto>), settings);

最新更新