我在谷歌上花了很长时间,所以如果这是一个简单的修复,请原谅我,但我在C#中找不到任何与此特定问题相关的解决方案。
在尝试使用以下代码时,它会给我一个错误:
string obj = JsonConvert.SerializeObject(playerObject);
Could not load type 'Newtonsoft.Json.JsonConvert' from assembly 'Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'.
我搜索到的唯一问题是人们遇到版本不匹配的错误,但据我所知这并不是我的问题。
完整代码如下:(“logHigh”是我使用的日志记录函数)
public void convertToObject(webPlayerObjArray playerObject)
{
logHigh("Entered function");
try
{
logHigh("Serializing");
string obj = JsonConvert.SerializeObject(playerObject);
logHigh(obj);
webPlayerObjArray plrobjarr = JsonConvert.DeserializeObject<webPlayerObjArray>(obj);
logHigh(plrobjarr.users[0].badgeName);
}catch (Exception e)
{
logHigh(e.Message);
}
logHigh("Finished function");
}
webPlayerObjArray playerFile = new webPlayerObjArray();
webObject webObj1 = new webObject();
webObj1.steamID = "test1";
webObj1.badgeName = "test2";
webObj1.weaponSkinName = "test3";
webObj1.sailName = "test4";
webObj1.cannonSkinName = "test5";
webObject webObj2 = new webObject();
webObj2.steamID = "test1";
webObj2.badgeName = "test2";
webObj2.weaponSkinName = "test3";
webObj2.sailName = "test4";
webObj2.cannonSkinName = "test5";
playerFile.users = new webObject[2];
playerFile.users[0] = webObj1;
playerFile.users[1] = webObj2;
logHigh(playerFile.users[0].badgeName);
logHigh(playerFile.users[1].badgeName);
类定义:
[System.Serializable]
public class webObject
{
public string steamID { get; set; }
public string badgeName { get; set; }
public string sailName { get; set; }
public string weaponSkinName { get; set; }
public string cannonSkinName { get; set; }
}
[System.Serializable]
public class webPlayerObjArray
{
public webObject[] users { get; set; }
}
使用Visual Studio 2019
通过NuGet包管理器添加了Newtonsoft
- 版本为12.0.3
对象声明是临时的。稍后将切换到从网页加载JSON,但我只是试图在第一次实例中使其正常工作,因此使用了这个函数名称。
可能有一个简单的解决方法,但不幸的是,我迄今为止无法找到它。
基于评论和测试的额外信息:
- 这是一个游戏mod的类库,而不是Web项目。
- 添加:
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.3" /> //12.0.3.23909 also had no effect
</dependentAssembly>
无论是否存在,app.config
都不起作用。
- 在VS 2019中,Newtonsoft.Json的引用没有任何警告。
- 重建项目没有效果。(必须这样做,因为一开始当我添加任何引用该DLL的代码后,我的函数就拒绝运行了。成功重建后,我最终遇到了上述错误。)
- 位于
bin\Debug
中的DLL版本与在VS 2019中引用的版本相同。 - 创建一个全新的项目并复制代码也不起作用。
- 更改目标框架(使用相应的DLL版本4.5或4.7.2)也没有效果。
- 尝试以下Newtonsoft.Json的先前版本,但都无法成功:
- 12.0.3
- 11.0.2
- 10.0.3
- 9.0.1
- 8.0.3
bin
和obj
文件夹并重新构建项目。确保根据您所在的Debug
或Release
环境删除正确的文件夹。 - Rui CaramalhoAlternion.csproj
包含一个链接<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL"> <HintPath>..\packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll</HintPath> </Reference>
。 - cheesenibbles