61得票10回答
忽略空属性在新的Net Core 3.0 Json中如何实现?

在ASP.Net Core 2.2中使用JSON.Net时,我能够在将其序列化为JSON时忽略属性值为空的属性:[JsonProperty(NullValueHandling = NullValueHandling.Ignore)] public DateTime? Created { get...

52得票2回答
System.Text.Json中等效的JsonProperty属性是什么?

在System.Text.Json中,等价于Newtonsoft.Json的JsonProperty属性是什么?示例:using Newtonsoft.Json; public class Example { [JsonProperty("test2")] ...

50得票7回答
System.Text.Json:使用自动转换反序列化JSON

如何使用 .Net Core 3 的新 System.Text.Json JsonSerializer 自动转换类型(例如,将 int 转换为 string,将 string 转换为 int)?例如,由于 JSON 中的 id 是数字,而 C#中的 Product.Id 期望一个字符串,因此以...

49得票8回答
.NET Core 3.0 JsonSerializer 如何填充现有对象?

我正在准备从ASP.NET Core 2.2迁移到3.0。 由于我不使用更高级的JSON功能(但可能会用到下面描述的一个),而3.0现在内置了JSON的命名空间/类,System.Text.Json,所以我决定看看能否放弃以前的默认选项Newtonsoft.Json。 请注意,我知道Syste...

47得票3回答
如何在 System.Text.Json.JsonSerializer 中使用类字段?

我最近将一个解决方案升级为全 .NET Core 3,我有一个类需要使用字段作为类变量。这是一个问题,因为新的 System.Text.Json.JsonSerializer 不支持序列化或反序列化字段,只支持处理属性。 是否有任何方法可以确保下面示例中的两个最终类具有完全相同的值? us...

45得票2回答
使用System.Text.Json,与JsonConverter相当的是什么?

我开始将一些代码从Newtonsoft.Json迁移到System.Text.Json在一个.NET Core 3.0 应用程序中。 我已经将属性从 [JsonProperty("id")]迁移到[JsonPropertyName("id")] 但是,我有一些带有JsonConverte...

41得票3回答
如何使用System.Text.Json对未知对象进行漂亮打印

使用System.Text.Json,我可以使用序列化选项来漂亮地打印JSON。 var options = new JsonSerializerOptions{ WriteIndented = true }; jsonString = JsonSerializer.Serialize(ty...

39得票8回答
ASP.NET Core 3中是否有一种内置的方法来使用snake case作为JSON命名策略?

我成功地使用以下代码使其正常工作:.AddNewtonsoftJson(options => { options.SerializerSettings.ContractResolver = new DefaultContractResolver { Nam...

37得票3回答
使用.NET core 3.0/System.text.Json解析JSON文件

我正在尝试使用.NET Core 3.0中的新JSON读取器System.Text.Json读取并解析一个无法在内存中容纳的大型JSON文件。 来自Microsoft的示例代码以ReadOnlySpan<byte>作为输入。 public static void Utf8J...

32得票4回答
System.Text.Json - 将嵌套对象反序列化为字符串

我正在尝试使用System.Text.Json.JsonSerializer部分反序列化模型,以便将其中一个属性读取为包含原始JSON的字符串。public class SomeModel { public int Id { get; set; } public string ...