C# 将json反序列化到列表时使用空指针

C# 将json反序列化到列表时使用空指针,c#,.net,json,.net-core-3.0,C#,.net,Json,.net Core 3.0,我正在尝试使用System.Text.JSON.JSON序列化器将JSON反序列化为ReportVersandLogdto列表 var reportVersandLogsAsync = JsonSerializer.Deserialize<List<ReportVersandLogDto>>(content, new JsonSerializerOptions { PropertyNameCaseInsensitive = true

我正在尝试使用
System.Text.JSON.JSON序列化器将JSON反序列化为ReportVersandLogdto列表

var reportVersandLogsAsync = JsonSerializer.Deserialize<List<ReportVersandLogDto>>(content, new JsonSerializerOptions {
                    PropertyNameCaseInsensitive = true,
                    IgnoreNullValues = true
                });
ReportVersandLogDto如下所示:

[
  {
    "AnzahlArtikel": 6,
    "Betreff": "Cupra Daily News",
    "ReportId": 379717,
    "ReportVersandLogId": 4244138,
    "VersendetAm": "2019-11-02T06:30:15.997",
    "Link": "foo"
  }
]
[JsonObject]
public class ReportVersandLogDto : IResource
{
    [JsonProperty("anzahlArtikel")]
    public long AnzahlArtikel { get; set; }

    [JsonProperty("betreff")]
    public string? Betreff { get; set; }

    [JsonProperty("hasBeenRead")]
    public bool HasBeenRead { get; set; }

    [JsonProperty("reportId")]
    public long ReportId { get; set; }

    [JsonProperty("reportVersandLogId")]
    public long ReportVersandLogId { get; set; }

    [JsonProperty("versendetAm")]
    public string? VersendetAm { get; set; }

    //[JsonProperty("link")]
    //public string? Link { get; set; }
}

不幸的是,在调用
JsonSerializer.Deserialize
方法时,我得到了一个NullPointerException

对象引用未设置为对象的实例

我不确定我做错了什么。。。你能给我指一下正确的方向吗


最小可复制示例: (这是一款.net core 3.0控制台应用程序)
我已经在上面发布了整个ReportVersandLogDto(它使用Newtonsoft.Json中的属性)

类程序
{
静态void Main(字符串[]参数)
{
var content=“\”[{\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\;
var reportVersandLogsAsync=JsonSerializer.Deserialize(内容,新JsonSerializerOptions{
PropertyNameCaseSensitive=true,
IgnoreNullValues=true
});
}
}

看起来您正在混合
Newtonsoft.Json
System.Text.Json

在Newtonsoft中使用

System.Text.Json
中是等效的

这些属性在
Newtonsoft.Json
System.Text.Json
之间不能互换。尝试将您的类更新到以下位置:

public class ReportVersandLogDto
{
    [JsonPropertyName("anzahlArtikel")]
    public long AnzahlArtikel { get; set; }

    [JsonPropertyName("betreff")]
    public string Betreff { get; set; }

    [JsonPropertyName("hasBeenRead")]
    public bool HasBeenRead { get; set; }

    [JsonPropertyName("reportId")]
    public long ReportId { get; set; }

    [JsonPropertyName("reportVersandLogId")]
    public long ReportVersandLogId { get; set; }

    [JsonPropertyName("versendetAm")]
    public string VersendetAm { get; set; }

    [JsonPropertyName("link")]
    public string Link { get; set; }
}
此外,除非在可空上下文中使用字符串,否则不需要将字符串声明为可空引用(
string?
)类型


请看这个。

看起来您正在混合
Newtonsoft.Json
System.Text.Json

在Newtonsoft中使用

System.Text.Json
中是等效的

这些属性在
Newtonsoft.Json
System.Text.Json
之间不能互换。尝试将您的类更新到以下位置:

public class ReportVersandLogDto
{
    [JsonPropertyName("anzahlArtikel")]
    public long AnzahlArtikel { get; set; }

    [JsonPropertyName("betreff")]
    public string Betreff { get; set; }

    [JsonPropertyName("hasBeenRead")]
    public bool HasBeenRead { get; set; }

    [JsonPropertyName("reportId")]
    public long ReportId { get; set; }

    [JsonPropertyName("reportVersandLogId")]
    public long ReportVersandLogId { get; set; }

    [JsonPropertyName("versendetAm")]
    public string VersendetAm { get; set; }

    [JsonPropertyName("link")]
    public string Link { get; set; }
}
此外,除非在可空上下文中使用字符串,否则不需要将字符串声明为可空引用(
string?
)类型


请查看此信息。

请提供我们可以复制并粘贴到控制台应用程序中。
JsonSerializer.Deserialize()
不会引发NullReferenceException。创建一个无效的JSON。您尝试过了吗?在您的示例中,您将JSON设置为JSON字符串。您的引号太多。请提供我们可以复制并粘贴到console应用程序中。
JsonSerializer.Deserialize()
不会引发NullReferenceException。创建一个无效的JSON。您尝试过了吗?在您的示例中,您将JSON设置为JSON字符串。你引用的话太多了。