반응형
asp.net core 1.0 web api는 camelcase를 사용합니다.
온RC2
같은 코드가 camel 대소문자를 포함한 json 형식을 반환합니다.netcore 1.0 출시 후 i가 새 프로젝트를 시작하고 동일한 코드가 소문자로 json을 반환합니다.
여러 솔루션을 시도했지만 web-api-serialize-properties-starting-from-lower-case-letter가 작동하지 않았습니다.
services
.AddMvc()
.AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver
= new Newtonsoft.Json.Serialization.DefaultContractResolver();
});
그러면 JSON 개체의 이름이 와 동일하게 유지됩니다.NET 클래스 속성
다음과 같이 JSON 동작을 설정할 수 있습니다.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver =
new CamelCasePropertyNamesContractResolver();
});
}
글로벌 레벨이 아닌 개개의 시리얼라이저 레벨에서도 실행할 수 있습니다.
예를 들어 컨트롤러 액션 방식에서 개체를 JSON으로 반환하려면 다음 작업을 수행합니다.
var jsonSerializerSettings = new JsonSerializerSettings { ContractResolver = new DefaultContractResolver() };
return new JsonResult(myObject, jsonSerializerSettings);
그 결과 생성되는 JSON 문자열은 예상되는 Pascal Case 에 일치합니다.NET 클래스/속성 이름
언급URL : https://stackoverflow.com/questions/38139607/asp-net-core-1-0-web-api-use-camelcase
반응형
'sourcecode' 카테고리의 다른 글
Pyspark: json 문자열 열을 구문 분석합니다. (0) | 2023.02.11 |
---|---|
html에서 data-signid 속성은 무엇입니까? (0) | 2023.02.11 |
웹 사이트 섹션의 도메인이 다릅니다. (0) | 2023.02.11 |
SQL 테이블을 python에서 JSON으로 반환 (0) | 2023.02.11 |
Retrofit & Gson을 사용한 JSON 어레이 응답 해석 (0) | 2023.02.11 |