編輯WeatherForecastController檔案
/// <summary>
/// 反序列化
/// </summary>
/// <returns></returns>
[HttpGet("JsonDeserialize")]
public ActionResult JsonDeserialize() {
var options = new JsonSerializerOptions {
PropertyNamingPolicy = null,
};
var jsonString = @"{""Name"":""中文名"",""Age"":18,""TemperatureCelsius"":52}";
var Result = JsonSerializer.Deserialize<TestClass>(jsonString,options);
return Ok(Result);
}
public class TestClass {
public string Name { get; set; }
public int Age { get; set; }
public int TemperatureCelsius { get; set; }
}
builder.Services.AddControllers()
.AddJsonOptions(options => {
//預設為小駝峰命名,將此參數改為null即可使用大駝峰命名
options.JsonSerializerOptions.PropertyNamingPolicy = null;
});