pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

      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>
      2. <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 class="tfjz1rf" id='lbSxY'><tfoot id='lbSxY'></tfoot><dl id='lbSxY'><fieldset id='lbSxY'></fieldset></dl></div>

        使用 json.net 在序列化期間合并兩個(gè)對(duì)象?

        Merge two objects during serialization using json.net?(使用 json.net 在序列化期間合并兩個(gè)對(duì)象?)
          <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 class="rfdtdpv" id='vo8l6'><tfoot id='vo8l6'></tfoot><dl id='vo8l6'><fieldset id='vo8l6'></fieldset></dl></div>

                  <tbody id='vo8l6'></tbody>

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

                • <bdo id='vo8l6'></bdo><ul id='vo8l6'></ul>
                • 本文介紹了使用 json.net 在序列化期間合并兩個(gè)對(duì)象?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  我遇到了以下情況,有人可以幫我實(shí)現(xiàn)以下目標(biāo)嗎?

                  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;}
                  }
                  

                  當(dāng)我使用 Json.net 對(duì) Sample 類的對(duì)象進(jìn)行序列化時(shí),得到如下結(jié)果,效果很好.

                  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":"..."
                  }
                  

                  推薦答案

                  你可以創(chuàng)建匿名對(duì)象并序列化它:

                  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 
                                });
                  

                  結(jié)果

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

                  但我建議您使用 Sample 類的默認(rèn)序列化,或創(chuàng)建將序列化為您的格式的類(即將 MyClass 屬性移動(dòng)到 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).

                  更新:您可以使用自定義轉(zhuǎn)換器,它將對(duì)象展平并將所有內(nèi)部對(duì)象屬性序列化為頂級(jí)對(duì)象屬性:

                  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());
                  

                  或者你可以簡(jiǎn)單地將匿名類型創(chuàng)建隱藏到自定義轉(zhuǎn)換器中,如果你只需要一種類型的這種行為:

                  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);
                      }
                  }
                  

                  這篇關(guān)于使用 json.net 在序列化期間合并兩個(gè)對(duì)象?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  Ignore whitespace while reading XML(讀取 XML 時(shí)忽略空格)
                  XML to LINQ with Checking Null Elements(帶有檢查空元素的 XML 到 LINQ)
                  Reading XML with unclosed tags in C#(在 C# 中讀取帶有未閉合標(biāo)簽的 XML)
                  Parsing tables, cells with Html agility in C#(在 C# 中使用 Html 敏捷性解析表格、單元格)
                  delete element from xml using LINQ(使用 LINQ 從 xml 中刪除元素)
                  Parse malformed XML(解析格式錯(cuò)誤的 XML)
                • <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 class="97tjtpb" 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>
                      <tbody id='dqTsd'></tbody>

                          1. 主站蜘蛛池模板: 不发火防静电金属骨料_无机磨石_水泥自流平_修补砂浆厂家「圣威特」 | 东亚液氮罐-液氮生物容器-乐山市东亚机电工贸有限公司 | 全自动翻转振荡器-浸出式水平振荡器厂家-土壤干燥箱价格-常州普天仪器 | 不锈钢复合板|钛复合板|金属复合板|南钢集团安徽金元素复合材料有限公司-官网 | 彼得逊采泥器-定深式采泥器-电动土壤采样器-土壤样品风干机-常州索奥仪器制造有限公司 | 分子精馏/精馏设备生产厂家-分子蒸馏工艺实验-新诺舜尧(天津)化工设备有限公司 | 制丸机,小型中药制丸机,全自动制丸机价格-甘肃恒跃制药设备有限公司 | 高柔性拖链电缆-聚氨酯卷筒电缆-柔性屏蔽电缆厂家-玖泰电缆 | 【同风运车官网】一站式汽车托运服务平台,验车满意再付款 | VI设计-LOGO设计公司-品牌设计公司-包装设计公司-导视设计-杭州易象设计 | 小区健身器材_户外健身器材_室外健身器材_公园健身路径-沧州浩然体育器材有限公司 | 塑料异型材_PVC异型材_封边条生产厂家_PC灯罩_防撞扶手_医院扶手价格_东莞市怡美塑胶制品有限公司 | 生物除臭剂-除味剂-植物-污水除臭剂厂家-携葵环保有限公司 | 奇酷教育-Python培训|UI培训|WEB大前端培训|Unity3D培训|HTML5培训|人工智能培训|JAVA开发的教育品牌 | 长沙广告公司|长沙广告制作设计|长沙led灯箱招牌制作找望城湖南锦蓝广告装饰工程有限公司 | 铸铝门厂家,别墅大门庭院大门,别墅铸铝门铜门[十大品牌厂家]军强门业 | 上海防爆真空干燥箱-上海防爆冷库-上海防爆冷柜?-上海浦下防爆设备厂家? | 2025世界机器人大会_IC China_半导体展_集成电路博览会_智能制造展览网 | 免联考国际MBA_在职MBA报考条件/科目/排名-MBA信息网 | 臻知网大型互动问答社区-你的问题将在这里得到解答!-无锡据风网络科技有限公司 | 裹包机|裹膜机|缠膜机|绕膜机-上海晏陵智能设备有限公司 | 冷却塔厂家_冷却塔维修_冷却塔改造_凉水塔配件填料公司- 广东康明节能空调有限公司 | 真空上料机(一种真空输送机)-百科 | Maneurop/美优乐压缩机,活塞压缩机,型号规格,技术参数,尺寸图片,价格经销商 | 铆钉机|旋铆机|东莞旋铆机厂家|鸿佰专业生产气压/油压/自动铆钉机 | 医学动画公司-制作3d医学动画视频-医疗医学演示动画制作-医学三维动画制作公司 | 英国公司注册-新加坡公司注册-香港公司开户-离岸公司账户-杭州商标注册-杭州优创企业 | 连续油炸机,全自动油炸机,花生米油炸机-烟台茂源食品机械制造有限公司 | 北京公寓出租网-北京酒店式公寓出租平台 | 杭州画室_十大画室_白墙画室_杭州美术培训_国美附中培训_附中考前培训_升学率高的画室_美术中考集训美术高考集训基地 | 面粉仓_储酒罐_不锈钢储酒罐厂家-泰安鑫佳机械制造有限公司 | 【直乐】河北石家庄脊柱侧弯医院_治疗椎间盘突出哪家医院好_骨科脊柱外科专业医院_治疗抽动症/关节病骨伤权威医院|排行-直乐矫形中医医院 | 武汉高低温试验箱_恒温恒湿试验箱厂家-武汉蓝锐环境科技有限公司 | 箱式破碎机_移动方箱式破碎机/价格/厂家_【华盛铭重工】 | 根系分析仪,大米外观品质检测仪,考种仪,藻类鉴定计数仪,叶面积仪,菌落计数仪,抑菌圈测量仪,抗生素效价测定仪,植物表型仪,冠层分析仪-杭州万深检测仪器网 | 扒渣机,铁水扒渣机,钢水扒渣机,铁水捞渣机,钢水捞渣机-烟台盛利达工程技术有限公司 | 蔬菜清洗机_环速洗菜机_异物去除清洗机_蔬菜清洗机_商用洗菜机 - 环速科技有限公司 | 温州中研白癜风专科_温州治疗白癜风_温州治疗白癜风医院哪家好_温州哪里治疗白癜风 | 京港视通报道-质量走进大江南北-京港视通传媒[北京]有限公司 | 深圳宣传片制作-企业宣传视频制作-产品视频拍摄-产品动画制作-短视频拍摄制作公司 | 电地暖-电采暖-发热膜-石墨烯电热膜品牌加盟-暖季地暖厂家 |