隐藏

.NET 6.0下关于Json解析的dll的相关问题

发布:2022/8/31 21:17:33作者:管理员 来源:本站 浏览次数:1472

问题遇到的现象和发生背景


System.Web.Extensions.dll和Newtonsoft.Json.dll在.NET 6.0下是不能用了吗?为什么找不到支持.NET6.0版本的dll,而且每次使用低版本就报下面这个错误:


img


Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly 'System.Security.Permissions, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. 系统找不到指定的文件。


File name: 'System.Security.Permissions, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'


  at Newtonsoft.Json.Serialization.JsonTypeReflector.get_DynamicCodeGeneration()


  at Newtonsoft.Json.Serialization.JsonTypeReflector.get_ReflectionDelegateFactory()


  at Newtonsoft.Json.Serialization.DefaultContractResolver.GetDefaultCreator(Type createdType)


  at Newtonsoft.Json.Serialization.DefaultContractResolver.InitializeContract(JsonContract contract)


  at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateObjectContract(Type objectType)


  at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateContract(Type objectType)


  at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)


  at Newtonsoft.Json.Utilities.ThreadSafeStore`2.Get(TKey key)


  at Newtonsoft.Json.Serialization.DefaultContractResolver.ResolveContract(Type type)


  at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.GetContract(Object value)


  at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.GetContractSafe(Object value)


  at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)


  at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)


  at Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value, Type objectType)


  at Newtonsoft.Json.JsonConvert.SerializeObjectInternal(Object value, Type type, JsonSerializer jsonSerializer)


  at Newtonsoft.Json.JsonConvert.SerializeObject(Object value, Type type, JsonSerializerSettings settings)


  at Newtonsoft.Json.JsonConvert.SerializeObject(Object value)


      at Game.MainClass.Main(String[] args) in E:\My Project Directory\Visual Studio Project\Test\Test\Program.cs:line 15


问题相关代码,请勿粘贴截图


using System;


using Newtonsoft.Json;




namespace Game


{


   class MainClass


   {


       public static void Main(string[] args)


       {


           Player player = new Player();


           player.id = 1;


           player.name = "player";


           player.coin = 100;




           string m1 = JsonConvert.SerializeObject(player);


           Player m2 = JsonConvert.DeserializeObject<Player>(m1);




           Console.WriteLine(m1);


           Console.WriteLine(m2.ToString());


       }


   }


}


.net6 使用JsonSerializer