在C#开发场景中,Nullable类型允许值类型变量赋值为null,常用于数据库字段映射、可选参数定义等场景。当需要对包含Nullable类型的数据进行序列化存储或者网络传输时,需要掌握正确的处理方式,避免数据转换出错。

Nullable类型基础说明
Nullable类型是System.Nullable<T>的语法糖,其中T必须是值类型。比如int?等价于Nullable<int>,可以存储int范围内的数值或者null。常见的Nullable类型包括int?、DateTime?、bool?等。
以下是一个简单的Nullable类型定义示例:
using System;
namespace NullableDemo
{
class Program
{
static void Main(string[] args)
{
// 定义可空整数类型
int? nullableInt = null;
// 定义可空日期类型
DateTime? nullableDate = DateTime.Now;
Console.WriteLine($"nullableInt: {nullableInt}");
Console.WriteLine($"nullableDate: {nullableDate}");
}
}
}
使用System.Text.Json处理Nullable类型
System.Text.Json是.NET Core 3.0及之后版本内置的序列化库,默认支持Nullable类型的序列化,不需要额外配置。序列化时,Nullable类型的值为null会直接序列化为JSON的null,有值则序列化为对应的值。
序列化示例
首先定义一个包含Nullable类型属性的实体类:
using System;
namespace NullableDemo
{
public class User
{
public int Id { get; set; }
public string Name { get; set; }
// 可空年龄属性
public int? Age { get; set; }
// 可空生日属性
public DateTime? Birthday { get; set; }
}
}
接下来进行序列化操作:
using System;
using System.Text.Json;
namespace NullableDemo
{
class Program
{
static void Main(string[] args)
{
User user = new User
{
Id = 1,
Name = "张三",
Age = null,
Birthday = DateTime.Parse("1990-01-01")
};
// 序列化对象
string json = JsonSerializer.Serialize(user);
Console.WriteLine("序列化结果:");
Console.WriteLine(json);
}
}
}
上述代码执行后,输出的JSON结果为:{"Id":1,"Name":"张三","Age":null,"Birthday":"1990-01-01T00:00:00"},可以看到Age属性因为是可空类型且值为null,直接序列化为JSON的null。
反序列化示例
将JSON字符串反序列化为包含Nullable类型的对象:
using System;
using System.Text.Json;
namespace NullableDemo
{
class Program
{
static void Main(string[] args)
{
string json = "{"Id":2,"Name":"李四","Age":25,"Birthday":null}";
// 反序列化JSON
User user = JsonSerializer.Deserialize<User>(json);
Console.WriteLine("反序列化结果:");
Console.WriteLine($"Id: {user.Id}");
Console.WriteLine($"Name: {user.Name}");
Console.WriteLine($"Age: {user.Age}");
Console.WriteLine($"Birthday: {user.Birthday}");
}
}
}
执行后可以看到,JSON中的null会被正确赋值给Birthday可空属性,Age的25会被正确赋值给Age可空属性。
使用Newtonsoft.Json处理Nullable类型
Newtonsoft.Json(也叫Json.NET)是.NET生态中广泛使用的第三方序列化库,同样默认支持Nullable类型的处理,使用方式和System.Text.Json类似。
序列化示例
首先通过NuGet安装Newtonsoft.Json包,然后执行以下序列化代码:
using System;
using Newtonsoft.Json;
namespace NullableDemo
{
class Program
{
static void Main(string[] args)
{
User user = new User
{
Id = 3,
Name = "王五",
Age = 30,
Birthday = null
};
// 序列化对象
string json = JsonConvert.SerializeObject(user);
Console.WriteLine("Newtonsoft.Json序列化结果:");
Console.WriteLine(json);
}
}
}
输出结果为:{"Id":3,"Name":"王五","Age":30,"Birthday":null},和System.Text.Json的处理结果一致。
反序列化示例
使用Newtonsoft.Json进行反序列化:
using System;
using Newtonsoft.Json;
namespace NullableDemo
{
class Program
{
static void Main(string[] args)
{
string json = "{"Id":4,"Name":"赵六","Age":null,"Birthday":"2000-05-01T00:00:00"}";
// 反序列化JSON
User user = JsonConvert.DeserializeObject<User>(json);
Console.WriteLine("Newtonsoft.Json反序列化结果:");
Console.WriteLine($"Id: {user.Id}");
Console.WriteLine($"Name: {user.Name}");
Console.WriteLine($"Age: {user.Age}");
Console.WriteLine($"Birthday: {user.Birthday}");
}
}
}
处理注意事项
- 如果自定义了序列化配置,不要错误地将Nullable类型的值强制转换为非可空类型,否则反序列化时如果JSON对应字段为null会抛出异常。
- 对于
bool?类型,序列化后JSON中的值为true、false或者null,反序列化时可以正确映射,不需要额外处理。 - 如果需要自定义Nullable类型的序列化格式,比如日期类型的可空值需要指定格式,可以在属性上添加对应的特性,两种序列化库都有对应的特性支持。
通过以上两种方式,都可以正确完成C#中Nullable类型的序列化和反序列化操作,开发者可以根据项目使用的序列化库选择对应的实现方式。
C#Nullable类型序列化反序列化JsonConvert修改时间:2026-07-04 15:18:41