.NET 9.0 中 System.Text.Json 的全面使用指南


以下是一些 System.Text.Json 在 .NET 9.0 中的使用方式,包括序列化、反序列化、配置选项等,并附上输出结果。

  • 基本序列化和反序列化
cs 复制代码
using System;
using System.Text.Json;
public class Program
{
    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
    public static void Main()
    {
        Person person = new Person { Name = "张三", Age = 30 };
        // 序列化
        string jsonString = JsonSerializer.Serialize(person);
        Console.WriteLine("序列化结果:");
        Console.WriteLine(jsonString);
        // 反序列化
        Person deserializedPerson = JsonSerializer.Deserialize<Person>(jsonString);
        Console.WriteLine("\n反序列化结果:");
        Console.WriteLine($"姓名:{deserializedPerson.Name}, 年龄:{deserializedPerson.Age}");
    }
}
  • 输出结果:
cs 复制代码
序列化结果:
{"Name":"张三","Age":30}
反序列化结果:
姓名:张三, 年龄:30
  • 使用配置选项
cs 复制代码
using System;
using System.Text.Json;
public class Program
{
    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
    public static void Main()
    {
        Person person = new Person { Name = "张三", Age = 30 };
        // 配置选项
        var options = new JsonSerializerOptions
        {
            PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
            WriteIndented = true
        };
        // 序列化
        string jsonString = JsonSerializer.Serialize(person, options);
        Console.WriteLine("序列化结果(使用配置选项):");
        Console.WriteLine(jsonString);
    }
}

输出结果:

cs 复制代码
序列化结果(使用配置选项):
{
  "name": "张三",
  "age": 30
}
  • 处理嵌套对象
cs 复制代码
using System;
using System.Text.Json;
public class Program
{
    public class Address
    {
        public string City { get; set; }
        public string Street { get; set; }
    }
    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public Address Address { get; set; }
    }
    public static void Main()
    {
        Person person = new Person
        {
            Name = "张三",
            Age = 30,
            Address = new Address { City = "北京", Street = "长安街" }
        };
        string jsonString = JsonSerializer.Serialize(person);
        Console.WriteLine("序列化结果(嵌套对象):");
        Console.WriteLine(jsonString);
    }
}

输出结果:

cs 复制代码
序列化结果(嵌套对象):
{"Name":"张三","Age":30,"Address":{"City":"北京","Street":"长安街"}}
  • 处理集合
cs 复制代码
using System;
using System.Collections.Generic;
using System.Text.Json;
public class Program
{
    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
    public static void Main()
    {
        List<Person> people = new List<Person>
        {
            new Person { Name = "张三", Age = 30 },
            new Person { Name = "李四", Age = 25 }
        };
        string jsonString = JsonSerializer.Serialize(people);
        Console.WriteLine("序列化结果(集合):");
        Console.WriteLine(jsonString);
    }
}

输出结果:

cs 复制代码
序列化结果(集合):
[{"Name":"张三","Age":30},{"Name":"李四","Age":25}]
相关推荐
小羊没烦恼!5 天前
微服务化的基石——持续集成
java·大数据·word·powerpoint·.net
小羊没烦恼!5 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
譕痕5 天前
JSONObject与JSONArray封装数据格式区别
java·json
神仙别闹5 天前
基于C#+MySQL实现(WinForm)个人聊天室软件
c#
A黄俊辉A5 天前
uniapp webview中实现 app和内嵌的H5双向通信
vue.js·json
kybs19915 天前
全球灾害数据分析可视化 毕业设计-附源码66794
vue.js·spring boot·mysql·安全·django·c#·asp.net
伞伞悦读5 天前
【第37期】Python JSON 与配置详解:序列化、反序列化、嵌套结构和配置文件
开发语言·python·json
Mikko75 天前
jackson-databind 升到 2.21.6 就安全了吗?jackson-core 是另一个坐标,它那条 high 全局库至今没收
java·后端·安全·json
Polevne5 天前
C# SFTP客户端实现文件传输
c#·ssh
samble5 天前
从零搭建灌装监控系统(四):深色主题与样式系统,ResourceDictionary 统一管理
c#·wpf·mvvm·样式·工业控制