我有以下代碼:
using System.Text.Json;
private async Task<AuditReviewerDelegationDto> GetDelegationAsync()
{
// TODO: Get current logged in user, pass mdmuseridentifier
var mdmUserIdentifier = 248113;
var response = LocalHttpClient.GetAsync(BaseAddress "api/AuditReviewerDelegation/GetDelegation/" mdmUserIdentifier).Result;
response.EnsureSuccessStatusCode();
var responseContent = await response.Content.ReadAsStringAsync();
var tt = JsonSerializer.Deserialize<AuditReviewerDelegationDto>(responseContent);
return tt;
}
我的變數 responseContent 具有我期望的所有資料。但是,當我使用 Deserialize 方法時,我的 tt 變數為空。我相信這樣做的原因是 responseContent 上的所有屬性都以小寫開頭,但在我的 AuditReviewerDelegationDto 上,它們都以大寫開頭。我不能將我的模型更改為小寫。無論如何,我可以讓解串器忽略大小寫或將回應物件設定為重新調整駱駝大小寫。
uj5u.com熱心網友回復:
是的,為了忽略屬性名稱的大小寫,您只需要設定JsonSerializerOptions.PropertyNameCaseInsensitive為true. 有關更多詳細資訊,請參閱此頁面,但代碼可能如下所示:
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
};
var tt = JsonSerializer.Deserialize<AuditReviewerDelegationDto>(responseContent, options);
但是,如果tt本身null(而不是個別屬性tt是null),你可能有另一個問題-這取決于內的實際文本responseContent。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/343642.html
下一篇:Nodejs無法提取JSON值
