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

      <legend id='0fkiG'><style id='0fkiG'><dir id='0fkiG'><q id='0fkiG'></q></dir></style></legend>

        <bdo id='0fkiG'></bdo><ul id='0fkiG'></ul>
    1. <small id='0fkiG'></small><noframes id='0fkiG'>

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

        我可以在屬性中指定路徑以將我的類中的屬性映

        Can I specify a path in an attribute to map a property in my class to a child property in my JSON?(我可以在屬性中指定路徑以將我的類中的屬性映射到我的 JSON 中的子屬性嗎?) - IT屋-程序員軟件開發技術分享社

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

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

            • <bdo id='bct3x'></bdo><ul id='bct3x'></ul>
            • <tfoot id='bct3x'></tfoot>

                • 本文介紹了我可以在屬性中指定路徑以將我的類中的屬性映射到我的 JSON 中的子屬性嗎?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  有一些代碼(我無法更改)使用 Newtonsoft.Json 的 DeserializeObject<T>(strJSONData) 從 Web 請求中獲取數據并將其轉換為類對象(我可以換班).通過使用 [DataMember(Name = "raw_property_name")] 裝飾我的類屬性,我可以將原始 JSON 數據映射到我的類中的正確屬性.有沒有辦法可以將 JSON 復雜對象的子屬性映射到簡單屬性?這是一個例子:

                  There is some code (which I can't change) that uses Newtonsoft.Json's DeserializeObject<T>(strJSONData) to take data from a web request and convert it to a class object (I can change the class). By decorating my class properties with [DataMember(Name = "raw_property_name")] I can map the raw JSON data to the correct property in my class. Is there a way I can map the child property of a JSON complex object to a simple property? Here's an example:

                  {
                      "picture": 
                      {
                          "id": 123456,
                          "data": 
                          {
                              "type": "jpg",
                              "url": "http://www.someplace.com/mypicture.jpg"
                          }
                      }
                  }
                  

                  除了 URL 之外,我不關心圖片對象的任何其余部分,因此不想在我的 C# 類中設置復雜的對象.我真的只是想要這樣的東西:

                  I don't care about any of the rest of the picture object except for URL, and so don't want to setup a complex object in my C# class. I really just want something like:

                  [DataMember(Name = "picture.data.url")]
                  public string ProfilePicture { get; set; }
                  

                  這可能嗎?

                  推薦答案

                  好吧,如果你只需要一個額外的屬性,一個簡單的方法是將你的 JSON 解析為 JObject,使用 ToObject()JObject 填充您的類,然后使用 SelectToken() 拉入額外的屬性.

                  Well, if you just need a single extra property, one simple approach is to parse your JSON to a JObject, use ToObject() to populate your class from the JObject, and then use SelectToken() to pull in the extra property.

                  所以,假設你的班級看起來像這樣:

                  So, assuming your class looked something like this:

                  class Person
                  {
                      [JsonProperty("name")]
                      public string Name { get; set; }
                  
                      [JsonProperty("age")]
                      public string Age { get; set; }
                  
                      public string ProfilePicture { get; set; }
                  }
                  

                  你可以這樣做:

                  string json = @"
                  {
                      ""name"" : ""Joe Shmoe"",
                      ""age"" : 26,
                      ""picture"":
                      {
                          ""id"": 123456,
                          ""data"":
                          {
                              ""type"": ""jpg"",
                              ""url"": ""http://www.someplace.com/mypicture.jpg""
                          }
                      }
                  }";
                  
                  JObject jo = JObject.Parse(json);
                  Person p = jo.ToObject<Person>();
                  p.ProfilePicture = (string)jo.SelectToken("picture.data.url");
                  

                  小提琴:https://dotnetfiddle.net/7gnJCK

                  如果您喜歡更花哨的解決方案,您可以制作自定義 JsonConverter 以使 JsonProperty 屬性的行為與您描述的一樣.轉換器需要在類級別上運行,并結合上述技術使用一些反射來填充所有屬性.以下是它在代碼中的樣子:

                  If you prefer a more fancy solution, you could make a custom JsonConverter to enable the JsonProperty attribute to behave like you describe. The converter would need to operate at the class level and use some reflection combined with the above technique to populate all the properties. Here is what it might look like in code:

                  class JsonPathConverter : JsonConverter
                  {
                      public override object ReadJson(JsonReader reader, Type objectType, 
                                                      object existingValue, JsonSerializer serializer)
                      {
                          JObject jo = JObject.Load(reader);
                          object targetObj = Activator.CreateInstance(objectType);
                  
                          foreach (PropertyInfo prop in objectType.GetProperties()
                                                                  .Where(p => p.CanRead && p.CanWrite))
                          {
                              JsonPropertyAttribute att = prop.GetCustomAttributes(true)
                                                              .OfType<JsonPropertyAttribute>()
                                                              .FirstOrDefault();
                  
                              string jsonPath = (att != null ? att.PropertyName : prop.Name);
                              JToken token = jo.SelectToken(jsonPath);
                  
                              if (token != null && token.Type != JTokenType.Null)
                              {
                                  object value = token.ToObject(prop.PropertyType, serializer);
                                  prop.SetValue(targetObj, value, null);
                              }
                          }
                  
                          return targetObj;
                      }
                  
                      public override bool CanConvert(Type objectType)
                      {
                          // CanConvert is not called when [JsonConverter] attribute is used
                          return false;
                      }
                  
                      public override bool CanWrite
                      {
                          get { return false; }
                      }
                  
                      public override void WriteJson(JsonWriter writer, object value,
                                                     JsonSerializer serializer)
                      {
                          throw new NotImplementedException();
                      }
                  }
                  

                  為了演示,我們假設 JSON 現在如下所示:

                  To demonstrate, let's assume the JSON now looks like the following:

                  {
                    "name": "Joe Shmoe",
                    "age": 26,
                    "picture": {
                      "id": 123456,
                      "data": {
                        "type": "jpg",
                        "url": "http://www.someplace.com/mypicture.jpg"
                      }
                    },
                    "favorites": {
                      "movie": {
                        "title": "The Godfather",
                        "starring": "Marlon Brando",
                        "year": 1972
                      },
                      "color": "purple"
                    }
                  }
                  

                  ...除了之前的信息之外,您還對該人最喜歡的電影(標題和年份)和最喜歡的顏色感興趣.您將首先使用 [JsonConverter] 屬性標記您的目標類以將其與自定義轉換器相關聯,然后在每個屬性上使用 [JsonProperty] 屬性,指定所需的屬性路徑(區分大小寫)作為名稱.目標屬性也不必是原語——您可以像我在這里對 Movie 所做的那樣使用子類(注意不需要介入 Favorites 類).

                  ...and you are interested in the person's favorite movie (title and year) and favorite color in addition to the information from before. You would first mark your target class with a [JsonConverter] attribute to associate it with the custom converter, then use [JsonProperty] attributes on each property, specifying the desired property path (case sensitive) as the name. The target properties don't have to be primitives either-- you can use a child class like I did here with Movie (and notice there's no intervening Favorites class required).

                  [JsonConverter(typeof(JsonPathConverter))]
                  class Person
                  {
                      [JsonProperty("name")]
                      public string Name { get; set; }
                  
                      [JsonProperty("age")]
                      public int Age { get; set; }
                  
                      [JsonProperty("picture.data.url")]
                      public string ProfilePicture { get; set; }
                  
                      [JsonProperty("favorites.movie")]
                      public Movie FavoriteMovie { get; set; }
                  
                      [JsonProperty("favorites.color")]
                      public string FavoriteColor { get; set; }
                  }
                  
                  // Don't need to mark up these properties because they are covered by the 
                  // property paths in the Person class
                  class Movie
                  {
                      public string Title { get; set; }
                      public int Year { get; set; }
                  }
                  

                  有了所有屬性,你就可以像往常一樣反序列化,它應該正常工作":

                  With all the attributes in place, you can just deserialize as normal and it should "just work":

                  Person p = JsonConvert.DeserializeObject<Person>(json);
                  

                  小提琴:https://dotnetfiddle.net/Ljw32O

                  這篇關于我可以在屬性中指定路徑以將我的類中的屬性映射到我的 JSON 中的子屬性嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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)

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

                    <bdo id='rhMp5'></bdo><ul id='rhMp5'></ul>
                        <tbody id='rhMp5'></tbody>
                      • <small id='rhMp5'></small><noframes id='rhMp5'>

                          <tfoot id='rhMp5'></tfoot>
                          <legend id='rhMp5'><style id='rhMp5'><dir id='rhMp5'><q id='rhMp5'></q></dir></style></legend>

                          1. 主站蜘蛛池模板: 日韩精品1区2区3区 成人黄页在线观看 | 日韩a v在线免费观看 | 国产精品福利在线 | 国产亚洲精品美女久久久久久久久久 | 妞干网福利视频 | 日韩成人一区 | 久久国产精99精产国高潮 | 亚洲欧美一区二区三区在线 | 亚洲精品三级 | 久久国产精品网站 | 国产一区二区三区 | 国产粉嫩尤物极品99综合精品 | 久久精品99国产精品日本 | 日韩网站在线观看 | 国产成人精品av | 红色av社区 | 亚洲婷婷一区 | 男女视频在线看 | 久久人爽爽人爽爽 | 亚洲精品久久久久久久久久久久久 | 黄色一级大片在线观看 | 成人日韩 | 久久久精品一区二区三区 | www天天操| 97久久精品午夜一区二区 | 欧美一区不卡 | 天天干天天草 | 人人性人人性碰国产 | 成年人在线观看视频 | 91麻豆精品国产91久久久更新资源速度超快 | аⅴ资源新版在线天堂 | 亚洲高清免费视频 | 亚洲精品专区 | 夜夜骑首页 | 黄色片在线 | 午夜免费在线电影 | 国产午夜亚洲精品不卡 | 欧美成人精品一区二区男人看 | 草久久| 亚洲综合一区二区三区 | 国产亚洲精品久久久久动 |