当前位置: 首页 > news >正文

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


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

  • 基本序列化和反序列化
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}");}
}
  • 输出结果:
序列化结果:
{"Name":"张三","Age":30}
反序列化结果:
姓名:张三, 年龄:30

  • 使用配置选项
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);}
}

输出结果:

序列化结果(使用配置选项):
{"name": "张三","age": 30
}
  • 处理嵌套对象
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);}
}

输出结果:

序列化结果(嵌套对象):
{"Name":"张三","Age":30,"Address":{"City":"北京","Street":"长安街"}}
  • 处理集合
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);}
}

输出结果:

序列化结果(集合):
[{"Name":"张三","Age":30},{"Name":"李四","Age":25}]

http://www.lryc.cn/news/486608.html

相关文章:

  • Python自动检测requests所获得html文档的编码
  • 11.12机器学习_特征工程
  • RAG经验论文《FACTS About Building Retrieval Augmented Generation-based Chatbots》笔记
  • 【配置后的基本使用】CMake基础知识
  • ollama+springboot ai+vue+elementUI整合
  • 【项目开发】理解SSL延迟:为何HTTPS比HTTP慢?
  • 2.STM32之通信接口《精讲》之USART通信
  • Bootstrap和jQuery开发案例
  • Qt 之 qwt和QCustomplot对比
  • 【STM32】MPU6050简介
  • Oracle 单机及 RAC 环境 归档模式及路径修改
  • 抽象java入门1.5.3.1——类的进阶
  • python——模块 迭代器 正则
  • QT仿QQ聊天项目,第三节,实现聊天界面
  • Linux-何为CentOS
  • C++中的 std::optional
  • 猫狗识别之BUG汇总
  • 【论文复现】自动化细胞核分割与特征分析
  • 排序算法 -快速排序
  • K8S 查看pod节点的磁盘和内存使用情况
  • 华为HCIP——MSTP/RSTP与STP的兼容性
  • AI 大模型如何重塑软件开发流程:现状与未来展望
  • 3步实现贪吃蛇
  • 华东师范大学数学分析第五版PDF习题答案上册及下册
  • MySQL之联合查询
  • [C/C++] 定位新表达式 placement new
  • 【MySQL】MySQL的笛卡尔积现象是什么?简单说说
  • 《InsCode AI IDE:编程新时代的引领者》
  • 微搭低代码私有化部署搭建教程
  • 【在Linux世界中追寻伟大的One Piece】多路转接epoll(续)