更新时间:2023-01-17 20:36:25
试试这个:
Container container = new Container
{
Data = new Dictionary<string, object> { { "Text", "Hello world" } }
};
string jsonText = JsonConvert.SerializeObject(container);
var obj = JsonConvert.DeserializeObject<ExpandoObject>(jsonText, new ExpandoObjectConverter());
我发现,这样做让我的 ExpandoObject
从调用 DeserializeObject
。我想与您所提供的代码的问题是,当你在提供一个 ExpandoObjectConverter
,你问 Json.Net
反序列化集装箱
,所以我会想象, ExpandoObjectConverter
不被使用。
I found that doing this got me an ExpandoObject
from the call to DeserializeObject
. I think the issue with the code you have provided is that while you are supplying an ExpandoObjectConverter
, you are asking Json.Net
to deserialize a Container
, so I would imagine that the ExpandoObjectConverter
is not being used.
编辑:
如果我装点数据
财产 [JsonConverter(typeof运算(ExpandoObjectConverter))]
,并使用代码:
If I decorate the Data
property with [JsonConverter(typeof(ExpandoObjectConverter))]
and use the code:
var obj = JsonConvert.DeserializeObject<Container>(jsonText);
然后数据
属性被反序列化到 ExpandoObject
,而 OBJ
是集装箱
。
Then the Data
property is deserialized to an ExpandoObject
, while obj
is a Container
.