<tfoot id='vo8l6'></tfoot>

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

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

        • <bdo id='vo8l6'></bdo><ul id='vo8l6'></ul>
      1. 使用 json.net 在序列化期间合并两个对象?

        时间:2023-08-23

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

            <tfoot id='lbSxY'></tfoot>

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

              • <bdo id='lbSxY'></bdo><ul id='lbSxY'></ul>
                • <i id='lbSxY'><tr id='lbSxY'><dt id='lbSxY'><q id='lbSxY'><span id='lbSxY'><b id='lbSxY'><form id='lbSxY'><ins id='lbSxY'></ins><ul id='lbSxY'></ul><sub id='lbSxY'></sub></form><legend id='lbSxY'></legend><bdo id='lbSxY'><pre id='lbSxY'><center id='lbSxY'></center></pre></bdo></b><th id='lbSxY'></th></span></q></dt></tr></i><div id='lbSxY'><tfoot id='lbSxY'></tfoot><dl id='lbSxY'><fieldset id='lbSxY'></fieldset></dl></div>
                  本文介绍了使用 json.net 在序列化期间合并两个对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我遇到了以下情况,有人可以帮我实现以下目标吗?

                  I met a situation as below could anybody help me achieve as below?

                  例如,如果我有课程:-

                  For Example, if I have the class:-

                  public class Sample
                  {
                      public String name {get;set;}
                      public MyClass myclass {get;set;}
                  }
                  

                  我的 Myclass 如下:

                  public class MyClass
                  {
                      public String p1 {get;set;}
                      public String p2 {get;set;}
                  }
                  

                  当我使用 Json.net 对 Sample 类的对象进行序列化时,得到如下结果,效果很好.

                  When I am using Json.net to Serialize the object of the class Sample,I got as below and it works well.

                  {
                   "name":"...",
                   "myclass":
                            {
                              "p1":"...",
                              "p2":"..."
                             }
                   }
                  

                  它是正确的,但我想知道是否有可能得到如下的 json 字符串?

                  Its correct but I wonder is it possible to get the json string as below?

                  {
                   "name":"...",
                   "p1":"...",
                   "p2":"..."
                  }
                  

                  推荐答案

                  你可以创建匿名对象并序列化它:

                  You can create anonymous object and serialize it:

                  var sample = new Sample { 
                      name = "Bob", 
                      myclass = new MyClass { 
                                  p1 = "x", 
                                  p2 = "y" 
                                }};
                  
                  string json = JsonConvert.SerializeObject(new { 
                                   sample.name, 
                                   sample.myclass.p1, 
                                   sample.myclass.p2 
                                });
                  

                  结果

                  {"name":"Bob","p1":"x","p2":"y"}
                  

                  但我建议您使用 Sample 类的默认序列化,或创建将序列化为您的格式的类(即将 MyClass 属性移动到 Sample).

                  But I suggest you either use default serialization of your Sample class, or create class which will be serialized into your format (i.e. move MyClass properties into Sample).

                  更新:您可以使用自定义转换器,它将对象展平并将所有内部对象属性序列化为顶级对象属性:

                  UPDATE: You can use custom converter, which flattens object and serializes all inner objects properties as top level object properties:

                  public class FlattenJsonConverter : JsonConverter
                  {
                      public override void WriteJson(JsonWriter writer, object value, 
                          JsonSerializer serializer)
                      {
                          JToken t = JToken.FromObject(value);
                          if (t.Type != JTokenType.Object)
                          {
                              t.WriteTo(writer);
                              return;
                          }
                  
                          JObject o = (JObject)t;
                          writer.WriteStartObject();
                          WriteJson(writer, o);
                          writer.WriteEndObject();
                      }
                  
                      private void WriteJson(JsonWriter writer, JObject value)
                      {
                          foreach (var p in value.Properties())
                          {
                              if (p.Value is JObject)
                                  WriteJson(writer, (JObject)p.Value);
                              else
                                  p.WriteTo(writer);
                          }
                      }
                  
                      public override object ReadJson(JsonReader reader, Type objectType, 
                         object existingValue, JsonSerializer serializer)
                      {
                          throw new NotImplementedException();
                      }
                  
                      public override bool CanConvert(Type objectType)
                      {
                          return true; // works for any type
                      }
                  }
                  

                  用法:

                  string json = JsonConvert.SerializeObject(sample, new FlattenJsonConverter());
                  

                  或者你可以简单地将匿名类型创建隐藏到自定义转换器中,如果你只需要一种类型的这种行为:

                  Or you can simply hide anonymous type creation into custom converter, if you need this behavior for one type only:

                  public class SampleJsonConverter : JsonConverter
                  {
                      public override void WriteJson(JsonWriter writer, 
                          object value, JsonSerializer serializer)
                      {
                          Sample sample = (Sample)value;
                          JToken t = JToken.FromObject(new { 
                                        sample.name, 
                                        sample.myclass.p1, 
                                        sample.myclass.p2 
                                     });
                  
                          t.WriteTo(writer);
                      }
                  
                      public override object ReadJson(JsonReader reader, Type objectType,
                          object existingValue, JsonSerializer serializer)
                      {
                          throw new NotImplementedException();
                      }
                  
                      public override bool CanConvert(Type objectType)
                      {
                          return objectType == typeof(Sample);
                      }
                  }
                  

                  这篇关于使用 json.net 在序列化期间合并两个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:动态 JContainer (JSON.NET) &amp;在运行时迭代属性 下一篇:为动态对象的属性返回 null 的空合并运算符

                  相关文章

                  最新文章

                • <small id='dqTsd'></small><noframes id='dqTsd'>

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