c# 如何将对象转换为json字符串

来源:这里教程网 时间:2026-02-21 17:37:46 作者:

System.Text.Json
序列化对象(.NET Core 3.0+ 推荐)

这是目前官方主推的方式,性能好、内存占用低、默认安全(不序列化私有字段或非公共属性,除非显式配置)。

确保目标类的属性是
public
且有 getter(否则默认不会被序列化)
若需包含私有字段,加
[JsonInclude]
或用
JsonSerializerOptions
配置
IncludeFields = true
处理循环引用会直接抛出
System.Text.Json.JsonException
,需提前断开引用或改用
Newtonsoft.Json
日期默认输出为 ISO 8601 格式(如
"2024-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
别名等特性,仍需它。

安装 NuGet 包:
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 getter:字段(
public string Name;
)不会被序列化,必须是属性(
public string Name { get; set; }
若类含不可序列化成员,加
[JsonIgnore]
(Newtonsoft)或
[JsonIgnore]
+
System.Text.Json.Serialization
命名空间(.NET 5+)
使用
dynamic
ExpandoObject
时,
System.Text.Json
默认不支持,得换
Newtonsoft.Json
Unity 用户注意:IL2CPP 构建下
System.Text.Json
可能因 AOT 限制失败,优先用 Json.NET

要不要手动处理缩进或编码?

一般不需要。调试时才开启缩进;中文乱码基本不存在——两个库默认都用 UTF-8 编码写入字符串。

美化输出(仅调试):
JsonSerializerOptions.WriteIndented = true
Formatting.Indented
想把 JSON 写入文件并确保 BOM?别手动加——
File.WriteAllText(path, json, Encoding.UTF8)
即可,UTF-8 不带 BOM 是标准做法
若接口要求严格小写 key,用
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
仍是更省心的选择。

相关推荐