<bdo id='8rXnc'></bdo><ul id='8rXnc'></ul>
      <tfoot id='8rXnc'></tfoot>
    1. <i id='8rXnc'><tr id='8rXnc'><dt id='8rXnc'><q id='8rXnc'><span id='8rXnc'><b id='8rXnc'><form id='8rXnc'><ins id='8rXnc'></ins><ul id='8rXnc'></ul><sub id='8rXnc'></sub></form><legend id='8rXnc'></legend><bdo id='8rXnc'><pre id='8rXnc'><center id='8rXnc'></center></pre></bdo></b><th id='8rXnc'></th></span></q></dt></tr></i><div id='8rXnc'><tfoot id='8rXnc'></tfoot><dl id='8rXnc'><fieldset id='8rXnc'></fieldset></dl></div>

      <small id='8rXnc'></small><noframes id='8rXnc'>

        <legend id='8rXnc'><style id='8rXnc'><dir id='8rXnc'><q id='8rXnc'></q></dir></style></legend>

      1. 将 JSON 递归反序列化为 IDictionary&lt;string,obje

        时间:2023-08-23
          • <bdo id='NPvAx'></bdo><ul id='NPvAx'></ul>
            <i id='NPvAx'><tr id='NPvAx'><dt id='NPvAx'><q id='NPvAx'><span id='NPvAx'><b id='NPvAx'><form id='NPvAx'><ins id='NPvAx'></ins><ul id='NPvAx'></ul><sub id='NPvAx'></sub></form><legend id='NPvAx'></legend><bdo id='NPvAx'><pre id='NPvAx'><center id='NPvAx'></center></pre></bdo></b><th id='NPvAx'></th></span></q></dt></tr></i><div id='NPvAx'><tfoot id='NPvAx'></tfoot><dl id='NPvAx'><fieldset id='NPvAx'></fieldset></dl></div>
              <tbody id='NPvAx'></tbody>

            <legend id='NPvAx'><style id='NPvAx'><dir id='NPvAx'><q id='NPvAx'></q></dir></style></legend>

                <tfoot id='NPvAx'></tfoot>

                <small id='NPvAx'></small><noframes id='NPvAx'>

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

                  问题描述

                  我正在尝试将一些旧作品转换为使用 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;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何使用 json.net 将 c# 通用列表转换为 json? 下一篇:使用 Json.net 将大量数据流式传输为 JSON 格式

                  相关文章

                  最新文章

                  1. <tfoot id='bFOTp'></tfoot>
                  2. <small id='bFOTp'></small><noframes id='bFOTp'>

                    <i id='bFOTp'><tr id='bFOTp'><dt id='bFOTp'><q id='bFOTp'><span id='bFOTp'><b id='bFOTp'><form id='bFOTp'><ins id='bFOTp'></ins><ul id='bFOTp'></ul><sub id='bFOTp'></sub></form><legend id='bFOTp'></legend><bdo id='bFOTp'><pre id='bFOTp'><center id='bFOTp'></center></pre></bdo></b><th id='bFOTp'></th></span></q></dt></tr></i><div id='bFOTp'><tfoot id='bFOTp'></tfoot><dl id='bFOTp'><fieldset id='bFOTp'></fieldset></dl></div>

                    <legend id='bFOTp'><style id='bFOTp'><dir id='bFOTp'><q id='bFOTp'></q></dir></style></legend>
                    • <bdo id='bFOTp'></bdo><ul id='bFOTp'></ul>