.NET 6 Text.Json範例
目的
建立新專案
設定新的專案
其他資訊
編輯WeatherForecastController檔案
/// <summary>
/// 序列化
/// </summary>
/// <returns></returns>
[HttpGet("JsonSerialize")]
public ActionResult JsonSerialize() {
var Test = new TestClass() {
Name = "中文名",
Age = 18
};
var Result = JsonSerializer.Serialize(Test);
return Ok(Result);
}
/// <summary>
/// 反序列化
/// </summary>
/// <returns></returns>
[HttpGet("JsonDeserialize")]
public ActionResult JsonDeserialize() {
var jsonString = @"{""Name"":""中文名"",""Age"":18}";
var Result = JsonSerializer.Deserialize<TestClass>(jsonString);
return Ok(Result);
}
public class TestClass {
public string Name { get; set; }
public int Age { get; set; }
}
執行結果
延伸問題
參考
範例檔
Last updated







