NET Core 3.0 System. Text. Json Camel Case 序列化

在 ASP.NET Core 3.0 Web API 项目中,如何指定 System.Text.Json序列化选项来将 Pascal Case 属性序列化/反序列化为 Camel Case,反之亦然?

给定一个具有 Pascal Case 属性的模型,例如:

public class Person
{
public string Firstname { get; set; }
public string Lastname { get; set; }
}

And code to use System.Text.Json to deserialize a JSON string to type of Person class:

var json = "{\"firstname\":\"John\",\"lastname\":\"Smith\"}";
var person = JsonSerializer.Deserialize<Person>(json);

Does not successfully deserialize unless JsonPropertyName is used with each property like:

public class Person
{
[JsonPropertyName("firstname")]
public string Firstname { get; set; }
[JsonPropertyName("lastname")]
public string Lastname { get; set; }
}

我在 startup.cs中尝试了以下方法,但在仍然需要 JsonPropertyName方面没有帮助:

services.AddMvc().AddJsonOptions(options =>
{
options.JsonSerializerOptions.DictionaryKeyPolicy = JsonNamingPolicy.CamelCase;
options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
});


// also the following given it's a Web API project


services.AddControllers().AddJsonOptions(options => {
options.JsonSerializerOptions.DictionaryKeyPolicy = JsonNamingPolicy.CamelCase;
options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
});

How can you set Camel Case serialize/deserialize in ASP.NET Core 3.0 using the new System.Text.Json namespace?

谢谢!

105732 次浏览

AddJsonOptions()将仅为 MVC 配置 System.Text.Json。如果您想在自己的代码中使用 JsonSerializer,应该将配置传递给它。

var options = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
};


var json = "{\"firstname\":\"John\",\"lastname\":\"Smith\"}";
var person = JsonSerializer.Parse<Person>(json, options);

您可以使用 PropertyNameCaseInsensitive。您需要将它作为参数传递给反序列化器。

var json = "{\"firstname\":\"John\",\"lastname\":\"Smith\"}";
var options = new JsonSerializerOptions() { PropertyNameCaseInsensitive = true };
var person = JsonSerializer.Deserialize<Person>(json, options);

其中(来自 医生) :

Gets or sets a value that determines whether a property's name uses a case-insensitive comparison during deserialization. The default value 是假的

因此,它没有指定 camelCase 或 PascalCase,但是它将使用不区分大小写的比较。


下面将配置 System.Text.Json for Json 通过控制器端点传递:

services.AddControllers()
.AddJsonOptions(options => {
options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
});

You can still set it application wide by installing Microsoft.AspNetCore.Mvc.NewtonsoftJson Nuget Package, which allows you to use the previous Json serializer implementation :

services.AddControllers()
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
});

点击此处查看答案: IMvcBuilder AddJsonOptions 在.Net Core 3.0中的位置?

startup.cs:

// keeps the casing to that of the model when serializing to json
// (default is converting to camelCase)
services.AddMvc()
.AddJsonOptions(options => options.JsonSerializerOptions.PropertyNamingPolicy = null);

这意味着您不需要导入 newtonsoft.json。

The only other option for options.JsonSerializerOptions.PropertyNamingPolicy is JsonNamingPolicy.CamelCase. There do not seem to be any other JsonNamingPolicy naming policy options, such as snake_case or PascalCase.

如果需要 camelCase序列化,请在 Startup.cs: 中使用此代码(例如 firstName)

services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
});

如果需要 PascalCase序列化,请使用 Startup.cs: 中的这段代码(例如 FirstName)

services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNamingPolicy= null;
);

试试这个!

ConfigureServices方法内的 StartUp.cs中写:

 services.AddMvc()
.AddJsonOptions(options =>
options.JsonSerializerOptions.PropertyNamingPolicy
= JsonNamingPolicy.CamelCase);

您需要诸如 Newtonsoft.Json.SerializationSystem.Text.Json之类的名称空间

.NET Core 7最小 API 解决方案

要防止在序列化期间将 pascal case 属性重命名为驼峰 case,请使用构建器服务属性的 ConfigureHttpJsonOptions 方法。

builder.Services.ConfigureHttpJsonOptions(options => options.SerializerOptions.PropertyNamingPolicy = null);

要强制转换为驼峰大小写(默认行为) ,请使用:

builder.Services.ConfigureHttpJsonOptions(options => options.SerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase);