Newtonsoft.Json json.net 可以丟棄了,微軟發布了 System.Text.Json 適用於.net 4.6.1 +,后悔了,建議.net5+項目使用


微軟發布了 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”的項目“Log\WindowsFormsApp2”有關的包“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:\work\MyApp\packages”中找到包“System.Buffers 4.5.1”
在“I:\work\MyApp\packages”中找到包“System.Memory 4.5.4”
在“I:\work\MyApp\packages”中找到包“System.Numerics.Vectors 4.5.0”
在“I:\work\MyApp\packages”中找到包“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:\work\MyApp\packages”中
已將程序包“System.Buffers.4.5.1”添加到“packages.config”
已將“System.Buffers 4.5.1”成功安裝到 WindowsFormsApp2
程序包“System.Numerics.Vectors.4.5.0”已存在於文件夾“I:\work\MyApp\packages”中
已將程序包“System.Numerics.Vectors.4.5.0”添加到“packages.config”
已將“System.Numerics.Vectors 4.5.0”成功安裝到 WindowsFormsApp2
程序包“System.Runtime.CompilerServices.Unsafe.5.0.0”已存在於文件夾“I:\work\MyApp\packages”中
已將程序包“System.Runtime.CompilerServices.Unsafe.5.0.0”添加到“packages.config”
已將“System.Runtime.CompilerServices.Unsafe 5.0.0”成功安裝到 WindowsFormsApp2
程序包“System.Memory.4.5.4”已存在於文件夾“I:\work\MyApp\packages”中
已將程序包“System.Memory.4.5.4”添加到“packages.config”
已將“System.Memory 4.5.4”成功安裝到 WindowsFormsApp2
正在將程序包“System.Text.Encodings.Web.5.0.0”添加到文件夾“I:\work\MyApp\packages”
已將程序包“System.Text.Encodings.Web.5.0.0”添加到文件夾“I:\work\MyApp\packages”
已將程序包“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:\work\MyApp\packages”
已將程序包“System.Threading.Tasks.Extensions.4.5.4”添加到文件夾“I:\work\MyApp\packages”
已將程序包“System.Threading.Tasks.Extensions.4.5.4”添加到“packages.config”
已將“System.Threading.Tasks.Extensions 4.5.4”成功安裝到 WindowsFormsApp2
正在將程序包“Microsoft.Bcl.AsyncInterfaces.5.0.0”添加到文件夾“I:\work\MyApp\packages”
已將程序包“Microsoft.Bcl.AsyncInterfaces.5.0.0”添加到文件夾“I:\work\MyApp\packages”
已將程序包“Microsoft.Bcl.AsyncInterfaces.5.0.0”添加到“packages.config”
已將“Microsoft.Bcl.AsyncInterfaces 5.0.0”成功安裝到 WindowsFormsApp2
正在將程序包“System.ValueTuple.4.5.0”添加到文件夾“I:\work\MyApp\packages”
已將程序包“System.ValueTuple.4.5.0”添加到文件夾“I:\work\MyApp\packages”
已將程序包“System.ValueTuple.4.5.0”添加到“packages.config”
已將“System.ValueTuple 4.5.0”成功安裝到 WindowsFormsApp2
正在將程序包“System.Text.Json.5.0.1”添加到文件夾“I:\work\MyApp\packages”
已將程序包“System.Text.Json.5.0.1”添加到文件夾“I:\work\MyApp\packages”
已將程序包“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快

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM