且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

反序列化属性作为使用JSON.NET的ExpandoObject

更新时间: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.