久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    1. <tfoot id='PGaJ6'></tfoot><legend id='PGaJ6'><style id='PGaJ6'><dir id='PGaJ6'><q id='PGaJ6'></q></dir></style></legend>
        <bdo id='PGaJ6'></bdo><ul id='PGaJ6'></ul>
    2. <small id='PGaJ6'></small><noframes id='PGaJ6'>

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

        Json.net 如何將對象序列化為值

        Json.net how to serialize object as value(Json.net 如何將對象序列化為值)

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

        <tfoot id='eoE68'></tfoot>

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

                    <tbody id='eoE68'></tbody>
                  <i id='eoE68'><tr id='eoE68'><dt id='eoE68'><q id='eoE68'><span id='eoE68'><b id='eoE68'><form id='eoE68'><ins id='eoE68'></ins><ul id='eoE68'></ul><sub id='eoE68'></sub></form><legend id='eoE68'></legend><bdo id='eoE68'><pre id='eoE68'><center id='eoE68'></center></pre></bdo></b><th id='eoE68'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='eoE68'><tfoot id='eoE68'></tfoot><dl id='eoE68'><fieldset id='eoE68'></fieldset></dl></div>
                  本文介紹了Json.net 如何將對象序列化為值的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我仔細閱讀了文檔、StackOverflow 等,似乎找不到這個...

                  I've pored through the docs, StackOverflow, etc., can't seem to find this...

                  我想做的是將一個簡單的值類型的對象序列化/反序列化為一個值,而不是一個對象,如下所示:

                  What I want to do is serialize/deserialize a simple value-type of object as a value, not an object, as so:

                  public class IPAddress
                  {
                      byte[] bytes;
                  
                      public override string ToString() {... etc.
                  }
                  
                  public class SomeOuterObject
                  {
                      string stringValue;
                      IPAddress ipValue;
                  }
                  
                  IPAddress ip = new IPAddress("192.168.1.2");
                  var obj = new SomeOuterObject() {stringValue = "Some String", ipValue = ip};
                  string json = JsonConverter.SerializeObject(obj);
                  

                  我想要的是讓 json 像這樣序列化:

                  What I want is for the json to serialize like this:

                  // json = {"someString":"Some String","ipValue":"192.168.1.2"} <- value serialized as value, not subobject
                  

                  不是 ip 成為嵌套對象的地方,例如:

                  Not where the ip becomes a nested object, ex:

                  // json = {"someString":"Some String","ipValue":{"value":"192.168.1.2"}}
                  

                  有人知道怎么做嗎?謝謝!(P.S. 我在一個龐大的遺留 .NET 代碼庫上使用 Json 序列化,所以我無法真正更改任何現有類型,但我可以擴充/分解/裝飾它們以促進 Json 序列化.)

                  Does anyone know how to do this? Thanks! (P.S. I am bolting Json serialization on a large hairy legacy .NET codebase, so I can't really change any existing types, but I can augment/factor/decorate them to facilitate Json serialization.)

                  推薦答案

                  您可以使用 IPAddress 類的自定義 JsonConverter 來處理這個問題.這是您需要的代碼:

                  You can handle this using a custom JsonConverter for the IPAddress class. Here is the code you would need:

                  public class IPAddressConverter : JsonConverter
                  {
                      public override bool CanConvert(Type objectType)
                      {
                          return (objectType == typeof(IPAddress));
                      }
                  
                      public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
                      {
                          return new IPAddress(JToken.Load(reader).ToString());
                      }
                  
                      public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
                      {
                          JToken.FromObject(value.ToString()).WriteTo(writer);
                      }
                  }
                  

                  然后,將 [JsonConverter] 屬性添加到您的 IPAddress 類中,您就可以開始了:

                  Then, add a [JsonConverter] attribute to your IPAddress class and you're ready to go:

                  [JsonConverter(typeof(IPAddressConverter))]
                  public class IPAddress
                  {
                      byte[] bytes;
                  
                      public IPAddress(string address)
                      {
                          bytes = address.Split('.').Select(s => byte.Parse(s)).ToArray();
                      }
                  
                      public override string ToString() 
                      { 
                          return string.Join(".", bytes.Select(b => b.ToString()).ToArray()); 
                      }
                  }
                  

                  這是一個工作演示:

                  class Program
                  {
                      static void Main(string[] args)
                      {
                          IPAddress ip = new IPAddress("192.168.1.2");
                          var obj = new SomeOuterObject() { stringValue = "Some String", ipValue = ip };
                          string json = JsonConvert.SerializeObject(obj);
                          Console.WriteLine(json);
                      }
                  }
                  
                  public class SomeOuterObject
                  {
                      public string stringValue { get; set; }
                      public IPAddress ipValue { get; set; }
                  }
                  

                  輸出:

                  {"stringValue":"Some String","ipValue":"192.168.1.2"}
                  

                  這篇關于Json.net 如何將對象序列化為值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

                  相關文檔推薦

                  Ignore whitespace while reading XML(讀取 XML 時忽略空格)
                  XML to LINQ with Checking Null Elements(帶有檢查空元素的 XML 到 LINQ)
                  Reading XML with unclosed tags in C#(在 C# 中讀取帶有未閉合標簽的 XML)
                  Parsing tables, cells with Html agility in C#(在 C# 中使用 Html 敏捷性解析表格、單元格)
                  delete element from xml using LINQ(使用 LINQ 從 xml 中刪除元素)
                  Parse malformed XML(解析格式錯誤的 XML)

                        <tfoot id='wD8eF'></tfoot>

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

                          • <bdo id='wD8eF'></bdo><ul id='wD8eF'></ul>
                            <i id='wD8eF'><tr id='wD8eF'><dt id='wD8eF'><q id='wD8eF'><span id='wD8eF'><b id='wD8eF'><form id='wD8eF'><ins id='wD8eF'></ins><ul id='wD8eF'></ul><sub id='wD8eF'></sub></form><legend id='wD8eF'></legend><bdo id='wD8eF'><pre id='wD8eF'><center id='wD8eF'></center></pre></bdo></b><th id='wD8eF'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='wD8eF'><tfoot id='wD8eF'></tfoot><dl id='wD8eF'><fieldset id='wD8eF'></fieldset></dl></div>
                              <tbody id='wD8eF'></tbody>
                            <legend id='wD8eF'><style id='wD8eF'><dir id='wD8eF'><q id='wD8eF'></q></dir></style></legend>
                          • 主站蜘蛛池模板: 超碰av人人| 中文字幕日韩一区 | 欧洲毛片 | 伊人狼人影院 | 亚洲人成人一区二区在线观看 | 91国内精品久久 | 日本高清精品 | 国产一区二区三区 | 放个毛片看看 | 国产日韩欧美激情 | av在线免费网 | 国产偷录视频叫床高潮对白 | 岛国在线免费观看 | 岛国av免费看 | 亚洲精品久久久一区二区三区 | 性高湖久久久久久久久 | 在线观看成人免费视频 | 久久久久久久一区 | 国产福利小视频 | 国产成人99av超碰超爽 | 日韩一级一区 | 日韩成人免费视频 | 亚洲国产精品一区二区三区 | 欧美色综合一区二区三区 | 无码一区二区三区视频 | 日本色婷婷 | 蜜桃一区二区三区在线 | 少妇淫片aaaaa毛片叫床爽 | 成人国产精品免费观看 | 国产91精品久久久久久久网曝门 | 欧美日韩一二三区 | 成人精品在线观看 | 久久精品国产一区二区电影 | 日韩精品一区二区久久 | 亚洲综合色视频在线观看 | 色综合视频| 91久久综合亚洲鲁鲁五月天 | 精品亚洲永久免费精品 | 91精品91久久久 | 先锋影音资源网站 | 日韩在线综合 |