将JObject转换为动态对象

时间:2022-12-26 16:11:00

I am calling a REST endpoint from C# and I am receiving json which gets serialized into an object. One of the properties on this object is a dynamic property. The value of the dynamic property is set as a anonymous object on the server site like this:

我从C#调用REST端点,我收到json,它被序列化为一个对象。此对象的一个​​属性是动态属性。动态属性的值在服务器站点上设置为匿名对象,如下所示:

myObject.MyDynamicProp = new { Id = "MyId2134", Name = "MyName" };

On the client site the value of the dynamic property from the json serialization is a JObject containing the following value:

在客户端站点上,json序列化的动态属性值是一个包含以下值的JObject:

{{
  "id": "MyId2134",
  "Name": "MyName"
}}

I would have expected to be able to access the properties like this:

我原以为能够访问这样的属性:

var s = myObject.MyDynamicProp.Name;

but it does not find the Name property instead I have to get the value like this:

但它没有找到Name属性,而是我必须得到这样的值:

var s = myObject.MyDynamicProp["Name"].Value;

I tried converting the JObject into a dynamic object like this but it returns JObject:

我尝试将JObject转换为这样的动态对象,但它返回JObject:

var dyn = myObject.MyDynamicProp.ToObject<dynamic>();

How can I convert the dynamic property value such that I can call its properties directly?

如何转换动态属性值,以便我可以直接调用其属性?

var s = myObject.MyDynamicProp.Name;

UPDATE ...

更新......

I ran the following

我跑了以下

 dynamic d = JsonConvert.DeserializeObject("{\"MyDynamicProp\": {\"id\": \"MyId2134\", \"Name\": \"MyName\"}}");
 string name = d.MyDynamicProp.Name;

Which gives me the following the error:

这给了我以下错误:

 {Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:    `Newtonsoft.Json.Linq.JObject' does not contain a definition for `MyDynamicProp'
  at Microsoft.Scripting.Interpreter.ThrowInstruction.Run (Microsoft.Scripting.Interpreter.InterpretedFrame frame) [0x00027]

I would like to add that this is an Xamarin iOS project and the code is located in a PCL library.

我想补充一点,这是一个Xamarin iOS项目,代码位于PCL库中。


I assumed there was a problem with my code but it looks like it is not possible to use dynamic types within a Xamarin iOS project. https://developer.xamarin.com/guides/ios/advanced_topics/limitations/

我认为我的代码存在问题,但看起来在Xamarin iOS项目中无法使用动态类型。 https://developer.xamarin.com/guides/ios/advanced_topics/limitations/

1 个解决方案

#1


15  

It is actually quite easy. Instead of using var use dynamic on your JObject and you will be fine:

实际上很容易。而不是在你的JObject上使用var use dynamic,你会没事的:

dynamic do = myObject.MyDynamicProp;

string name = do.Name;

From your fragment:

从您的片段:

dynamic d = JsonConvert.DeserializeObject("{\"MyDynamicProp\": {\"id\": \"MyId2134\", \"Name\": \"MyName\"}}");
string name = d.MyDynamicProp.Name;

Console.WriteLine(name); // writes MyName

Why this works: As Richard explained, JObject derives indirectly from JToken which implements IDynamicMetaObjectProvider. It is that interface that allows dynamic to work.

为什么会这样:正如Richard解释的那样,JObject间接地从实现IDynamicMetaObjectProvider的JToken派生。正是这个界面允许动态工作。

#1


15  

It is actually quite easy. Instead of using var use dynamic on your JObject and you will be fine:

实际上很容易。而不是在你的JObject上使用var use dynamic,你会没事的:

dynamic do = myObject.MyDynamicProp;

string name = do.Name;

From your fragment:

从您的片段:

dynamic d = JsonConvert.DeserializeObject("{\"MyDynamicProp\": {\"id\": \"MyId2134\", \"Name\": \"MyName\"}}");
string name = d.MyDynamicProp.Name;

Console.WriteLine(name); // writes MyName

Why this works: As Richard explained, JObject derives indirectly from JToken which implements IDynamicMetaObjectProvider. It is that interface that allows dynamic to work.

为什么会这样:正如Richard解释的那样,JObject间接地从实现IDynamicMetaObjectProvider的JToken派生。正是这个界面允许动态工作。