ASP.NET Core 3.0 시스템Text.Json Camel 케이스 시리얼화
ASP.NET Core 3.0 Web API 프로젝트에서 시스템을 지정하려면 어떻게 해야 합니다.Pascal Case 속성을 Camel Case에 자동으로 직렬화/비직렬화하는 Json 시리얼화 옵션
다음과 같은 Pascal Case 속성을 가진 모델이 주어진 경우:
public class Person
{
public string Firstname { get; set; }
public string Lastname { get; set; }
}
그리고 시스템을 사용할 코드.Text.Json을 사용하여 JSON 문자열을 다음 유형으로 역직렬화합니다.Person클래스:
var json = "{\"firstname\":\"John\",\"lastname\":\"Smith\"}";
var person = JsonSerializer.Deserialize<Person>(json);
다음과 같이 각 속성에 JsonPropertyName을 사용하지 않는 한 직렬화를 해제하지 않습니다.
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;
});
ASP에서 Camel Case serialize/deserialize를 설정하려면 어떻게 해야 합니다.새로운 시스템을 사용한NET Core 3.0Text.Json 네임스페이스?
감사합니다!
AddJsonOptions()설정하다System.Text.JsonMVC 전용입니다.사용하고 싶은 경우JsonSerializer자신의 코드로 설정을 전달합니다.
var options = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
};
var json = "{\"firstname\":\"John\",\"lastname\":\"Smith\"}";
var person = JsonSerializer.Parse<Person>(json, options);
네가 원한다면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;
);
인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을 Import할 필요가 없습니다.
의 다른 유일한 옵션options.JsonSerializerOptions.PropertyNamingPolicy이JsonNamingPolicy.CamelCase다른 것은 없는 것 같다.JsonNamingPolicy정책 옵션(snake_case 또는 PascalCase 등)을 지정합니다.
사용할 수 있습니다.PropertyNameCaseInsensitive디시리얼라이저에 파라미터로 전달해야 합니다.
var json = "{\"firstname\":\"John\",\"lastname\":\"Smith\"}";
var options = new JsonSerializerOptions() { PropertyNameCaseInsensitive = true };
var person = JsonSerializer.Deserialize<Person>(json, options);
(문서로부터):
역직렬화 중에 속성 이름이 대소문자를 구분하지 않는 비교를 사용할지 여부를 결정하는 값을 가져오거나 설정합니다.기본값은 false 입니다.
따라서 camelCase 또는 PascalCase를 지정하지 않고 대소문자를 구분하지 않는 비교를 사용합니다.
다음은 시스템을 구성합니다.Text.Json for Json이 컨트롤러 끝점을 통과했습니다.
services.AddControllers()
.AddJsonOptions(options => {
options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
});
를 인스톨 하면, 애플리케이션 전체를 설정할 수 있습니다.Microsoft.AspNetCore.Mvc.NewtonsoftJsonNuget Package: 이전 Json 시리얼라이저 구현을 사용할 수 있습니다.
services.AddControllers()
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
});
Credits to Poke, answer here :IMvcBuilder AddJsonOptions는 어디에 있습니까?넷코어 3.0?
.NET Core 7 최소 API 솔루션
직렬화 중에 파스칼 케이스 속성의 이름이 camel 케이스로 변경되지 않도록 하려면 빌더의 서비스 속성의 ConfigureHttpJsonOptions 메서드를 사용합니다.
builder.Services.ConfigureHttpJsonOptions(options => options.SerializerOptions.PropertyNamingPolicy = null);
camel case 로의 변환을 강제하려면(디폴트 동작) 다음과 같이 합니다.
builder.Services.ConfigureHttpJsonOptions(options => options.SerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase);
이거 먹어봐!
»StartUp.cs내 the의 ConfigureServices다음 중 하나:
services.AddMvc()
.AddJsonOptions(options =>
options.JsonSerializerOptions.PropertyNamingPolicy
= JsonNamingPolicy.CamelCase);
요. Newtonsoft.Json.Serialization&System.Text.Json
언급URL : https://stackoverflow.com/questions/58476681/asp-net-core-3-0-system-text-json-camel-case-serialization
'source' 카테고리의 다른 글
| create-react-app을 사용할 때 사용자 지정 빌드 출력 폴더 사용 (0) | 2023.03.28 |
|---|---|
| JavaScript: Ajax 요청 후 글로벌 변수 (0) | 2023.03.28 |
| 리액트 라우터에서 일반 앵커링크를 사용하는 방법 (0) | 2023.03.28 |
| LinqPad에서 변경 내용을 제출하는 방법 (0) | 2023.03.28 |
| SELECT 문의 Oracle 열 이름 변경 (0) | 2023.03.28 |