更新时间:2022-11-22 20:15:35
试试这个:
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());
我发现这样做可以从对 DeserializeObject
的调用中获得一个 ExpandoObject
.我认为您提供的代码的问题在于,当您提供 ExpandoObjectConverter
时,您要求 Json.Net
反序列化 Container
,所以我认为 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))]
装饰 Data
属性并使用代码:
If I decorate the Data
property with [JsonConverter(typeof(ExpandoObjectConverter))]
and use the code:
var obj = JsonConvert.DeserializeObject<Container>(jsonText);
然后将 Data
属性反序列化为一个 ExpandoObject
,而 obj
是一个 Container
.
Then the Data
property is deserialized to an ExpandoObject
, while obj
is a Container
.