我似乎无法解决这个问题.我在同一问题上找到的所有其他问题都针对Newtonsoft.Json.
我使用外部提供的json API,该API返回字典.但如果它是空的,它们就会返回一个空数组:
有数据:
"booking/index": {
"key": {
data...
}
}
没有数据:
"booking/index": []
因此System.json解串器失败,出现这个错误(这是可以理解的):
System.Text.Json.JsonException:JNON值无法转换为System.Collections.Generic.Dictionary ' 2[System.字符串,checkfront_reports.Models.Api. BookingIndexObj].路径:$ð ' booking/index ']|线路号:0|字节位置InLine:408.
这里是我进行的调用和数据模型供参考:
var bookingIndex = await httpClient.GetFromJsonAsync<BookingIndexObj>($"booking/index?start_date={startDate.ToString("yyyy-MM-dd")}&end_date={endDate.ToString("yyyy-MM-dd")}");
public class BookingIndexObj
{
public BookingRequestObj Request { get; set; }
[JsonPropertyName("booking/index")]
public Dictionary<string, BookingIndexIndexObj> BookingIndex { get; set; }
}
public class BookingIndexIndexObj
{
public string? Code { get; set; }
[JsonPropertyName("status_id")]
public string? StatusId { get; set; }
}
public class BookingRequestObj
{
public int Page { get; set; }
public int Pages { get; set; }
}