将 JSON 递归反序列化为 IDictionary<string,object>

Deserialize JSON recursively to IDictionarylt;string,objectgt;(将 JSON 递归反序列化为 IDictionarylt;string,objectgt;)

本文介绍了将 JSON 递归序列化为 IDictionary<string,object>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些旧作品转换为使用 Newtonsoft JSON.NET.使用 System.Web.Script.Serialization.JavaScriptSerializer.Deserialize 方法的默认处理(例如,如果没有指定目标类型)是返回 Dictionary用于内部对象.

I'm trying to convert some older work to use Newtonsoft JSON.NET. The default handling using the System.Web.Script.Serialization.JavaScriptSerializer.Deserialize method (e.g. if no target type is specified) is to return a Dictionary<string,object> for inner objects.

这实际上是一个非常有用的 JSON 基本类型,因为它也恰好是 ExpandoObjects 使用的底层类型,并且是动态类型最明智的内部实现.

This is actually a really useful basic type for JSON since it also happens to be the underlying type used by ExpandoObjects and is the most sensible internal implementation for dynamic types.

如果我指定这种类型,例如:

If I specify this type, e.g.:

 var dict = JsonConvert.DeserializeObject<Dictionary<string,object>>(json);

JSON.NET 将正确反序列化最外层的对象结构,但它会为任何内部结构返回 JObject 类型.我真正需要的是将相同的外部结构用于任何内部对象类型结构.

JSON.NET will deserialize the outermost object structure correctly, but it returns a JObject type for any inner structures. What I really need is for the same outer structure to be used for any inner object-type structures.

有没有办法指定用于内部对象的类型,而不仅仅是返回的最外层类型?

Is there a way to specify a type to be used for inner objects, and not just the outermost type returned?

推荐答案

使用Json反序列化复杂对象时,需要添加一个JsonSerializer Settings作为参数.这将确保所有内部类型都能正确反序列化.

When Deserializing your complex objects using Json, you need to add a JsonSerializer Settings as a parameter. This will ensure that all of the inner types get deserialized properly.

    private JsonSerializerSettings _jsonSettings = new JsonSerializerSettings
    {
        TypeNameHandling = TypeNameHandling.All,
        TypeNameAssemblyFormat = FormatterAssemblyStyle.Full
    };

序列化对象时,可以使用 SerializerSettings:

When Serializing your object, you can use the SerializerSettings:

    string json= JsonConvert.SerializeObject(myObject, _jsonSettings)

然后在反序列化时,使用:

Then when you are deserializing, use:

    var dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(json, _jsonSettings);

另外,当你序列化时,将 JsonSerializerSettings 添加到你的 SerializeObject(object, settings)

Also, when you serialize, add the JsonSerializerSettings to your SerializeObject(object, settings)

如果需要,您还可以更改 TypeNameHandling 和 TypeNameAssemblyFormat.我将它们分别设置为全部"和完整",以确保我的复杂对象毫无疑问地正确序列化和反序列化,但智能感知为您提供其他选择

You can also change the TypeNameHandling and TypeNameAssemblyFormat if you need to. I have them set at 'All' and 'Full' respectively to ensure that my complex objects get serialized and deserialized properly without doubt, but intellisense provides you with other alternatives

这篇关于将 JSON 递归反序列化为 IDictionary&lt;string,object&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:将 JSON 递归反序列化为 IDictionary&lt;string,object&gt;

基础教程推荐