相关疑难解决方法(0)

.NET Core:从API JSON响应中删除空字段

在.NET Core 1.0的全局级别(所有API响应)中,如何配置Startup.cs以便在JSON响应中删除/忽略空字段?

使用Newtonsoft.Json,您可以将以下属性应用于属性,但我希望避免将其添加到每个属性:

[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string FieldName { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string OtherName { get; set; }
Run Code Online (Sandbox Code Playgroud)

serialization json .net-core asp.net-core-1.0

41
推荐指数
7
解决办法
2万
查看次数

.Net Core 3.0 JsonSerializer填充现有对象

我正在准备从ASP.NET Core 2.2迁移到3.0。

As I don't use any more advanced JSON features (but maybe one as described below), and 3.0 now comes with a built-in namespace/classes for JSON, System.Text.Json, I decided to see if I could drop the previous default Newtonsoft.Json. Do note, I'm aware that System.Text.Json will not completely replace Newtonsoft.Json.

I managed to do that everywhere, e.g.

var obj = JsonSerializer.Parse<T>(jsonstring);

var jsonstring = JsonSerializer.ToString(obj);
Run Code Online (Sandbox Code Playgroud)

but in one place, where I populate an existing object. …

c# asp.net-core razor-pages asp.net-core-3.0 system.text.json

36
推荐指数
3
解决办法
8311
查看次数