<tfoot id='YrpYq'></tfoot>

      1. <legend id='YrpYq'><style id='YrpYq'><dir id='YrpYq'><q id='YrpYq'></q></dir></style></legend>

          <bdo id='YrpYq'></bdo><ul id='YrpYq'></ul>

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

      2. <i id='YrpYq'><tr id='YrpYq'><dt id='YrpYq'><q id='YrpYq'><span id='YrpYq'><b id='YrpYq'><form id='YrpYq'><ins id='YrpYq'></ins><ul id='YrpYq'></ul><sub id='YrpYq'></sub></form><legend id='YrpYq'></legend><bdo id='YrpYq'><pre id='YrpYq'><center id='YrpYq'></center></pre></bdo></b><th id='YrpYq'></th></span></q></dt></tr></i><div id='YrpYq'><tfoot id='YrpYq'></tfoot><dl id='YrpYq'><fieldset id='YrpYq'></fieldset></dl></div>
      3. 使用 JSON.NET 序列化对象时如何添加自定义根节点?

        How can I add a custom root node when serializing an object with JSON.NET?(使用 JSON.NET 序列化对象时如何添加自定义根节点?)

          <bdo id='Osa9N'></bdo><ul id='Osa9N'></ul>
        • <small id='Osa9N'></small><noframes id='Osa9N'>

            • <tfoot id='Osa9N'></tfoot>

              1. <i id='Osa9N'><tr id='Osa9N'><dt id='Osa9N'><q id='Osa9N'><span id='Osa9N'><b id='Osa9N'><form id='Osa9N'><ins id='Osa9N'></ins><ul id='Osa9N'></ul><sub id='Osa9N'></sub></form><legend id='Osa9N'></legend><bdo id='Osa9N'><pre id='Osa9N'><center id='Osa9N'></center></pre></bdo></b><th id='Osa9N'></th></span></q></dt></tr></i><div id='Osa9N'><tfoot id='Osa9N'></tfoot><dl id='Osa9N'><fieldset id='Osa9N'></fieldset></dl></div>
                    <tbody id='Osa9N'></tbody>
                  <legend id='Osa9N'><style id='Osa9N'><dir id='Osa9N'><q id='Osa9N'></q></dir></style></legend>
                  本文介绍了使用 JSON.NET 序列化对象时如何添加自定义根节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我为我的一些对象添加了一个自定义属性,如下所示:

                  I have added a custom property to some of my objects like this:

                  [JsonCustomRoot("status")]
                  public class StatusDTO 
                  {
                      public int StatusId { get; set; }
                      public string Name { get; set; }
                      public DateTime Created { get; set; }
                  }
                  

                  属性很简单:

                  public class JsonCustomRoot :Attribute
                  {
                      public string rootName { get; set; }
                  
                      public JsonCustomRoot(string rootName)
                      {
                          this.rootName = rootName;
                      }
                  }
                  

                  序列化对象实例时 JSON.NET 的默认输出如下:

                  The default output from JSON.NET when serializing an instance of an object is this:

                  {"StatusId":70,"Name":"Closed","Created":"2012-12-12T11:50:56.6207193Z"}
                  

                  现在的问题是:如何使用自定义属性的值向 JSON 添加根节点,如下所示:

                  {status:{"StatusId":70,"Name":"Closed","Created":"2012-12-12T11:50:56.6207193Z"}}
                  

                  我发现有几篇文章提到了 IContractResolver 界面,但我无法掌握如何操作.我的尝试包括这段未完成的代码:

                  I have found several articles mentioning the IContractResolver interface, but I cannot grasp how to do it. My attempts include this unfinished piece of code:

                  protected override JsonObjectContract CreateObjectContract(Type objectType)
                  {
                      JsonObjectContract contract = base.CreateObjectContract(objectType);
                  
                      var info = objectType.GetCustomAttributes()
                                     .SingleOrDefault(t => (Type)t.TypeId==typeof(JsonCustomRoot));
                      if (info != null)
                      {
                          var myAttribute = (JsonCustomRoot)info;
                          // How can i add myAttribute.rootName to the root from here?
                          // Maybe some other method should be overrided instead?
                      }
                  
                      return contract;
                  }
                  

                  推荐答案

                  这是一个专门针对 Web API 的解决方案,我也在使用:RootFormatter.cs

                  Here's a solution specifically for Web API, which I am also using: RootFormatter.cs

                  我是根据 为 ASP.NET Web API 创建 JSONP 格式化程序.

                  我没有使用自定义属性,而是重用了 JsonObjectAttribute 的 Title 字段.这是一个使用代码:

                  Instead of using a custom attribute I am reusing Title field of JsonObjectAttribute. Here's a usage code:

                  using Newtonsoft.Json
                  
                  [JsonObject(Title = "user")]
                  public class User
                  {
                      public string mail { get; set; }
                  }
                  

                  然后,将 RootFormatter 添加到您的 App_Start 并在 WebApiConfig 中注册如下:

                  Then, add RootFormatter to your App_Start and register it as follows in WebApiConfig:

                  GlobalConfiguration.Configuration.Formatters.Insert(0, new RootFormatter());
                  

                  我能够获得类似于 WCF 的 WebMessageBodyStyle.Wrapped 的包装响应:

                  I was able to get a wrapped response similar to WCF's WebMessageBodyStyle.Wrapped:

                  {"user":{
                    "mail": "foo@example.com"
                  }}
                  

                  这篇关于使用 JSON.NET 序列化对象时如何添加自定义根节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Force JsonConvert.SerializeXmlNode to serialize node value as an Integer or a Boolean(强制 JsonConvert.SerializeXmlNode 将节点值序列化为整数或布尔值)
                  Using JSON to Serialize/Deserialize TimeSpan(使用 JSON 序列化/反序列化 TimeSpan)
                  Could not determine JSON object type for type quot;Classquot;(无法确定类型“Class的 JSON 对象类型.)
                  How to deserialize a JSONP response (preferably with JsonTextReader and not a string)?(如何反序列化 JSONP 响应(最好使用 JsonTextReader 而不是字符串)?)
                  how to de-serialize JSON data in which Timestamp it-self contains fields?(如何反序列化时间戳本身包含字段的JSON数据?)
                  JSON.Net custom contract serialization and Collections(JSON.Net 自定义合约序列化和集合)
                    <legend id='AGDtz'><style id='AGDtz'><dir id='AGDtz'><q id='AGDtz'></q></dir></style></legend>
                  1. <i id='AGDtz'><tr id='AGDtz'><dt id='AGDtz'><q id='AGDtz'><span id='AGDtz'><b id='AGDtz'><form id='AGDtz'><ins id='AGDtz'></ins><ul id='AGDtz'></ul><sub id='AGDtz'></sub></form><legend id='AGDtz'></legend><bdo id='AGDtz'><pre id='AGDtz'><center id='AGDtz'></center></pre></bdo></b><th id='AGDtz'></th></span></q></dt></tr></i><div id='AGDtz'><tfoot id='AGDtz'></tfoot><dl id='AGDtz'><fieldset id='AGDtz'></fieldset></dl></div>
                          <tbody id='AGDtz'></tbody>

                        • <tfoot id='AGDtz'></tfoot>

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

                            <bdo id='AGDtz'></bdo><ul id='AGDtz'></ul>