.NET 6 解決System.Text.Json序列化後會將所有非ASCII轉為Unicode
目的
建立新專案
設定新的專案
其他資訊
編輯WeatherForecastController檔案
[HttpGet("JsonSerialize")]
public ActionResult JsonSerialize() {
var options = new JsonSerializerOptions {
//美化輸出,會有空白字元
WriteIndented = true,
//將所有語言都不進行轉換
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
};
var Test = new TestClass() {
Name = "中文名",
Age = 18,
};
var Result = JsonSerializer.Serialize(Test, options);
return Ok(Result);
}
public class TestClass {
public string Name { get; set; }
public int Age { get; set; }
}
執行結果
參考
範例檔
Last updated




