微软发布了 System.Text.Json 适用于.net 4.6.1 +
只需在项目中 nuget 引用即可
using System; using System.Text.Json; using System.Windows; namespace WpfApp1 { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void haha_click(object sender, RoutedEventArgs e) { var aa=new WeatherForecast(); var json = Serialize(aa); MessageBox.Show(json); var aaa = JsonSerializer.Deserialize<WeatherForecast>(json); } string Serialize(WeatherForecast value) { return JsonSerializer.Serialize(value); } } public class WeatherForecast { public DateTimeOffset Date { get; set; } public DateTime Date1 { get; set; }=DateTime.Now; public int TemperatureC { get; set; } public string Summary { get; set; } } }
安装System.Text.Json 的同时需要安装其他共计9个包,如下,实在体验不好,还是不用了吧。
正在尝试收集与目标为“.NETFramework,Version=v4.6.1”的项目“LogWindowsFormsApp2”有关的包“System.Text.Json.5.0.1”的依赖项信息
收集依赖项信息花费时间 3.53 sec
正在尝试解析程序包“System.Text.Json.5.0.1”的依赖项,DependencyBehavior 为“Lowest”
解析依赖项信息花费时间 0 ms
正在解析操作以安装程序包“System.Text.Json.5.0.1”
已解析操作以安装程序包“System.Text.Json.5.0.1”
从“nuget.org”检索包“Microsoft.Bcl.AsyncInterfaces 5.0.0”
在“I:workMyApppackages”中找到包“System.Buffers 4.5.1”
在“I:workMyApppackages”中找到包“System.Memory 4.5.4”
在“I:workMyApppackages”中找到包“System.Numerics.Vectors 4.5.0”
在“I:workMyApppackages”中找到包“System.Runtime.CompilerServices.Unsafe 5.0.0”
从“nuget.org”检索包“System.Text.Encodings.Web 5.0.0”
从“nuget.org”检索包“System.Text.Json 5.0.1”
从“nuget.org”检索包“System.Threading.Tasks.Extensions 4.5.4”
从“nuget.org”检索包“System.ValueTuple 4.5.0”
程序包“System.Buffers.4.5.1”已存在于文件夹“I:workMyApppackages”中
已将程序包“System.Buffers.4.5.1”添加到“packages.config”
已将“System.Buffers 4.5.1”成功安装到 WindowsFormsApp2
程序包“System.Numerics.Vectors.4.5.0”已存在于文件夹“I:workMyApppackages”中
已将程序包“System.Numerics.Vectors.4.5.0”添加到“packages.config”
已将“System.Numerics.Vectors 4.5.0”成功安装到 WindowsFormsApp2
程序包“System.Runtime.CompilerServices.Unsafe.5.0.0”已存在于文件夹“I:workMyApppackages”中
已将程序包“System.Runtime.CompilerServices.Unsafe.5.0.0”添加到“packages.config”
已将“System.Runtime.CompilerServices.Unsafe 5.0.0”成功安装到 WindowsFormsApp2
程序包“System.Memory.4.5.4”已存在于文件夹“I:workMyApppackages”中
已将程序包“System.Memory.4.5.4”添加到“packages.config”
已将“System.Memory 4.5.4”成功安装到 WindowsFormsApp2
正在将程序包“System.Text.Encodings.Web.5.0.0”添加到文件夹“I:workMyApppackages”
已将程序包“System.Text.Encodings.Web.5.0.0”添加到文件夹“I:workMyApppackages”
已将程序包“System.Text.Encodings.Web.5.0.0”添加到“packages.config”
已将“System.Text.Encodings.Web 5.0.0”成功安装到 WindowsFormsApp2
正在将程序包“System.Threading.Tasks.Extensions.4.5.4”添加到文件夹“I:workMyApppackages”
已将程序包“System.Threading.Tasks.Extensions.4.5.4”添加到文件夹“I:workMyApppackages”
已将程序包“System.Threading.Tasks.Extensions.4.5.4”添加到“packages.config”
已将“System.Threading.Tasks.Extensions 4.5.4”成功安装到 WindowsFormsApp2
正在将程序包“Microsoft.Bcl.AsyncInterfaces.5.0.0”添加到文件夹“I:workMyApppackages”
已将程序包“Microsoft.Bcl.AsyncInterfaces.5.0.0”添加到文件夹“I:workMyApppackages”
已将程序包“Microsoft.Bcl.AsyncInterfaces.5.0.0”添加到“packages.config”
已将“Microsoft.Bcl.AsyncInterfaces 5.0.0”成功安装到 WindowsFormsApp2
正在将程序包“System.ValueTuple.4.5.0”添加到文件夹“I:workMyApppackages”
已将程序包“System.ValueTuple.4.5.0”添加到文件夹“I:workMyApppackages”
已将程序包“System.ValueTuple.4.5.0”添加到“packages.config”
已将“System.ValueTuple 4.5.0”成功安装到 WindowsFormsApp2
正在将程序包“System.Text.Json.5.0.1”添加到文件夹“I:workMyApppackages”
已将程序包“System.Text.Json.5.0.1”添加到文件夹“I:workMyApppackages”
已将程序包“System.Text.Json.5.0.1”添加到“packages.config”
已将“System.Text.Json 5.0.1”成功安装到 WindowsFormsApp2
执行 nuget 操作花费时间 7.54 sec
已用时间: 00:00:11.4537578
========== 已完成 ==========
建议.net5+项目使用,速度要比Newtonsoft.Json快