.NET Core 3.0+ 推荐使用 System.Text.Json,性能好、内存占用低、默认安全,仅序列化 public 属性(需 getter),私有字段需 [JsonInclude] 或 IncludeFields=true;循环引用会抛异常,需提前处理;日期默认 ISO 8601 格式;Newtonsoft.Json 更灵活,支持循环引用忽略、自定义转换器、私有成员序列化等,适合旧项目或复杂需求。
System.Text.
Json 序列化对象(.NET Core 3.0+ 推荐)这是目前官方主推的方式,性能好、内存占用低、默认安全(不序列化私有字段或非公共属性,除非显式配置)。
public 且有 getter(否则默认不会被序列化)[JsonInclude] 或用 JsonSerializerOptions 配置 IncludeFields = true
System.Text.Json.JsonException,需提前断开引用或改用 Newtonsoft.Json
"2025-05-20T14:30:00"),无需额外配置using System.Text.Json;
var person = new { Name = "Alice", Age = 30 };
string json = JsonSerializer.Serialize(person); // → {"Name":"Alice","Age":30}
Newtonsoft.Json(Json.NET)序列化(兼容旧项目/需要高级功能)如果你在 .NET Framework 项目中,或依赖 ReferenceLoopHandling、自定义转换器、JsonProperty 别名等特性,仍需它。
Newtonsoft.Json
JsonSerializerSettings)ReferenceLoopHandling.Ignore 静默跳过,但可能丢失数据DateTime 默认按本地时区格式化,易引发时区混淆,建议显式设 DateFormatHandling.IsoDateFormat
using Newtonsoft.Json;
var person = new { Name = "Bob", BirthDate = DateTime.Now };
string json = JsonConvert.SerializeObject(person,
new JsonSerializerSettings { DateFormatHandling = DateFormatHandling.IsoDateFormat });
NotSupportedException 或空 JSON(如 {})怎么办?常见于属性无 getter、类型不可序列化(如 IntPtr、委托、未标记 [Serializable] 的自定义类)、或用了匿名类型但运行时环境限制。
public string Name;)不会被序列化,必须是属性(public string Name { get; set; })[JsonIgnore](Newtonsoft)或 [JsonIgnore] + System.Text.Json.Serialization 命名空间(.NET 5+)dynamic 或 ExpandoObject 时,System.Text.Json 默认不支持,得换 Newtonsoft.Json
System.Text.Json 可能因 AOT 限制失败,优先用 Json.NET一般不需要。调试时才开启缩进;中文乱码基本不存在——两个库默认都用 UTF-8 编码写入字符串。
JsonSerializerOptions.WriteIndented = true 或 Formatting.Indented
File.WriteAllText(path, json, Encoding.UTF8) 即可,UTF-8 不带 BOM 是标准做法JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase
var options = new JsonSerializerOptions { WriteIndented = true, PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
string json = JsonSerializer.Serialize(person, options); // → { "name": "Alice", "age": 30 }
实际用哪个,取决于你用的 .NET 版本、是否已有 Json.NET 依赖、以及有没有循环引用或动态类型需求。.NET 6+ 新项目,闭眼用 System.Text.Json;老项目或要快速绕过序列化限制,Newtonsoft.Json 仍是更省心的选择。