C# Newtonsoft JSON - 动态对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13683757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:25:47  来源:igfitidea点击:

Newtonsoft JSON - Dynamic Objects

c#jsondynamicdeserializationjson.net

提问by MrEyes

I am using the Newtonsoft JSON library to perform dynamic deserialisation on incoming raw JSON and have found something that I just can't explain.

我正在使用 Newtonsoft JSON 库对传入的原始 JSON 执行动态反序列化,并发现了一些我无法解释的东西。

The starting point is the following JSON string:

起点是以下 JSON 字符串:

{
  "task": {
    "dueDate": "2012-12-03T00:00:00"
  }
}

Nothing too complex there...

没有什么太复杂的......

In code I am then doing this:

在代码中,我正在这样做:

var dyn = JsonConvert.DeserializeObject<dynamic>(rawJson);
DateTime dueDate = dyn.task.dueDate.Value;

This code has been in place for months and works fine, however in a recent test build we were seeing the following error:

此代码已存在数月且工作正常,但是在最近的测试版本中,我们看到以下错误:

'Newtonsoft.Json.Linq.JObject' does not contain a definition for 'task'

Stack Trace: at CallSite.Target(Closure , CallSite , Object ) at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)

“Newtonsoft.Json.Linq.JObject”不包含“任务”的定义

堆栈跟踪:在 CallSite.Target(Closure , CallSite , Object ) 在 System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)

Now this is where is gets odd, everything starts to work again if I change the code above from:

现在这是奇怪的地方,如果我将上面的代码从以下位置更改,一切都会重新开始工作:

DateTime dueDate = dyn.task.dueDate.Value;

to

DateTime dueDate = dyn["task"]["dueDate"].Value;

So, although this is "fixed" I don't understand why this fixes it and what the possible cause could be. Does anybody have any ideas

所以,虽然这是“固定的”,但我不明白为什么这会修复它以及可能的原因是什么。有没有人有任何想法

回答by Hylaean

You can try this:

你可以试试这个:

dynamic task = JObject.Parse(rawJson);

Documentation: Querying JSON with dynamic

文档:使用动态查询 JSON

回答by BlackTigerX

This is definitely some referencing issue, I just had the same problem, turned out the issue was that I had added the packages folder to the project so that I could check it in in VSS (sorry, mandatory), as soon as I removed the packages folder from visual studio, it started working fine

这绝对是一些引用问题,我只是遇到了同样的问题,原来问题是我已将包文件夹添加到项目中,以便我可以在 VSS 中检入它(抱歉,强制),只要我删除了来自visual studio的packages文件夹,它开始工作正常

回答by v.shashenko

I had the same problem with an app using Json.NET. The issue recurred only on one specific machine and it turned out that the machine had another version of Json.NET installed in GAC. After removing it from GAC the app started to work correctly.

我在使用 Json.NET 的应用程序中遇到了同样的问题。该问题仅在一台特定机器上再次出现,结果该机器在 GAC 中安装了另一个版本的 Json.NET。从 GAC 中删除后,该应用程序开始正常工作。

回答by Shaun Wilson

In my case it turned out I was catching first chance exceptions, so although this exception was being thrown it was not, in fact, preventing my code from executing.

在我的情况下,结果证明我正在捕获第一次机会异常,所以尽管抛出了这个异常,但实际上并没有阻止我的代码执行。

It would still be nice to know why this happens for some content deserialized by Json.NET, but not all content. For example, I have a dynamic object i serialize with json.net in a unit test, then deserialize the serialized content. The serialized content looks identical to content which, at run time, causes runtime binder exceptions to be thrown.

知道为什么 Json.NET 反序列化的某些内容会发生这种情况仍然很高兴,但不是所有内容。例如,我有一个动态对象,我在单元测试中用 json.net 序列化,然后反序列化序列化的内容。序列化的内容看起来与在运行时导致抛出运行时绑定器异常的内容相同。

回答by DaNeSh

I had the same problem.

我有同样的问题。

Json.NET 6.0.4 cause this issue.You should change it to Json.Net 6.0.5. None of the project should reference to Json.NET 6.0.4 .

Json.NET 6.0.4 导致此问题。您应该将其更改为 Json.Net 6.0.5。该项目不应引用 Json.NET 6.0.4 。

Go to Tools -> NuGet Package Manager -> Manage NuGet Packages for solution... and check, if any of the projects use Json.NET 6.0.4, fix them.

转到 Tools -> NuGet Package Manager -> Manage NuGet Packages for solution... 并检查是否有任何项目使用 Json.NET 6.0.4,修复它们。

回答by E Perl

I had this issue going on for a while, turns out, the DLL used was compiled for .NET 3.5 which did not support dynamic.... Make sure your DLL is compiled for .NET 4.5 and above

我有这个问题持续了一段时间,事实证明,使用的 DLL 是为不支持动态的 .NET 3.5 编译的....确保您的 DLL 是为 .NET 4.5 及更高版本编译的