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

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

        <legend id='oDtuE'><style id='oDtuE'><dir id='oDtuE'><q id='oDtuE'></q></dir></style></legend>

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

        <tfoot id='oDtuE'></tfoot>

      1. 收到錯(cuò)誤“無(wú)法從 Newtonsoft.Json.Linq.JProperty 添加或

        Getting the error quot;Cannot add or remove items from Newtonsoft.Json.Linq.JPropertyquot; in Json.net(收到錯(cuò)誤“無(wú)法從 Newtonsoft.Json.Linq.JProperty 添加或刪除項(xiàng)目在 Json.net 中)

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

              <bdo id='6vG32'></bdo><ul id='6vG32'></ul>
              <legend id='6vG32'><style id='6vG32'><dir id='6vG32'><q id='6vG32'></q></dir></style></legend>
            • <small id='6vG32'></small><noframes id='6vG32'>

              <tfoot id='6vG32'></tfoot>

                    <tbody id='6vG32'></tbody>

                  本文介紹了收到錯(cuò)誤“無(wú)法從 Newtonsoft.Json.Linq.JProperty 添加或刪除項(xiàng)目"在 Json.net 中的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

                  問(wèn)題描述

                  所以我試圖通過(guò)將 json 對(duì)象作為 JObject 讀取,刪除一些字段,然后使用 Json.Net.問(wèn)題是,每當(dāng)我嘗試刪除字段時(shí),都會(huì)收到錯(cuò)誤消息:

                  So I'm trying to control deserialization by reading a json object as a JObject, deleting some fields, and then deserializing it again to my target object using Json.Net. The problem is, any time I try to delete a field, I get the error:

                  Newtonsoft.Json.JsonException"類型的未處理異常發(fā)生在 Newtonsoft.Json.dll

                  An unhandled exception of type 'Newtonsoft.Json.JsonException' occurred in Newtonsoft.Json.dll

                  附加信息:無(wú)法添加或刪除項(xiàng)目Newtonsoft.Json.Linq.JProperty.

                  Additional information: Cannot add or remove items from Newtonsoft.Json.Linq.JProperty.

                  這是我的(簡(jiǎn)化,但仍然導(dǎo)致錯(cuò)誤)代碼:

                  Here's my (simplified, but still causing the error) code:

                  JToken token = (JToken)JsonConvert.DeserializeObject(File.ReadAllText(fileName));
                  
                  foreach (JToken inner in token["docs"])
                  {
                      if (inner["_id"] != null)
                          inner["_id"].Remove();
                  
                      MyObject read = new MyObject();
                      JsonConvert.PopulateObject(inner.ToString(), read);
                      Values.Add((MyObject)JsonConvert.DeserializeObject(inner.ToString(), typeof(MyObject)));
                  }
                  

                  json 是一個(gè)非常大的文件,其中 docs 數(shù)組包含許多元素,如下所示(為清楚起見(jiàn)再次簡(jiǎn)化):

                  The json is a very large file where the docs array contains many elements as follows (again simplified for clarity):

                  {
                      "docs": [
                          {
                              "Time": "None",
                              "Level": 1,
                              "_id": "10208"              
                          },
                          {
                              "Time": "None",
                              "Level": 1,
                              "_id": "10209"
                          }
                      ]
                  }
                  

                  或者,如果有更好的方法將 JSON 反序列化為特定類型,但仍然忽略其他字段,那將是一個(gè)不錯(cuò)的選擇.

                  Alternatively if there's a better way to deserialize JSON to a specific type, but still ignoring additional fields, that would be a fine alternative.

                  推薦答案

                  假設(shè) Values 是一個(gè) List 和你的 MyObject 類看起來(lái)像這樣:

                  Assuming Values is a List<MyObject> and your MyObject class looks like this:

                  class MyObject
                  {
                      public string Time { get; set; }
                      public int Level { get; set; }
                  }
                  

                  您可以將所有代碼替換為以下代碼以獲得您想要的結(jié)果:

                  you can replace all that code with the following to get the result you want:

                  string json = File.ReadAllText(fileName);
                  Values = JToken.Parse(json)["docs"].ToObject<List<MyObject>>();
                  

                  這是因?yàn)?Json.Net 默認(rèn)會(huì)忽略缺失的屬性.由于 MyObject 類不包含要反序列化的 _id 屬性,因此您無(wú)需費(fèi)力嘗試將其從 JSON 中刪除.

                  This works because Json.Net will ignore missing properties by default. Since the MyObject class does not contain an _id property to deserialize into, you don't need to jump through hoops trying to remove it from the JSON.

                  Remove() 不起作用的原因說(shuō)明

                  Explanation of why Remove() didn't work

                  JToken.Remove() 從其父級(jí)中刪除 JToken.從其父 JObject 中刪除 JProperty 或從 JArray 中刪除子 JToken 是合法的.但是,您不能從 JProperty 中刪除該值.JProperty 必須始終只有一個(gè)值.

                  JToken.Remove() removes a JToken from its parent. It is legal to remove a JProperty from its parent JObject, or to remove a child JToken from a JArray. However, you cannot remove the value from a JProperty. A JProperty must always have exactly one value.

                  當(dāng)您請(qǐng)求 token["_id"] 時(shí),您會(huì)返回名為 _idJPropertyvalue>,而不是 JProperty 本身.因此,如果您嘗試對(duì)該值調(diào)用 Remove(),您將收到錯(cuò)誤消息.要使其按照您的方式工作,您需要像這樣使用 Parent:

                  When you ask for token["_id"] you get back the value of the JProperty called _id, not the JProperty itself. Therefore you will get an error if you try to call Remove() on that value. To make it work the way you are doing, you'd need to use Parent like this:

                  if (inner["_id"] != null)
                      inner["_id"].Parent.Remove();
                  

                  這表示找到名稱為 _id 的屬性并給我值.如果存在,獲取該值的父級(jí)(屬性),并將其從其父級(jí)(包含 JObject)."

                  This says "Find the property whose name is _id and give me the value. If it exists, get that value's parent (the property), and remove it from its parent (the containing JObject)."

                  更直接的方法是使用 Property() 方法直接訪問(wèn)屬性.但是,此方法僅適用于 JObject,不適用于 JToken,因此您需要將 inner 的聲明更改為 JObject 或強(qiáng)制轉(zhuǎn)換:

                  A more straightforward way to do it is to use the Property() method to access the property directly. However, this method is only available on JObject, not JToken, so you would either need to change the declaration of inner to a JObject or cast it:

                  foreach (JObject inner in token["docs"].Children<JObject>())
                  {
                      JProperty idProp = inner.Property("_id");
                      if (idProp != null)
                          idProp.Remove();
                      ...
                  }
                  

                  最后,正如評(píng)論中提到的,如果您使用的是 C# 6 或更高版本,您可以使用空條件運(yùn)算符稍微縮短代碼:

                  Lastly, as mentioned in the comments, if you're using C# 6 or later you can shorten the code a bit using the null-conditional operator:

                      inner.Property("_id")?.Remove();
                  

                  這篇關(guān)于收到錯(cuò)誤“無(wú)法從 Newtonsoft.Json.Linq.JProperty 添加或刪除項(xiàng)目"在 Json.net 中的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(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)
                      <bdo id='08rU1'></bdo><ul id='08rU1'></ul>

                          <tbody id='08rU1'></tbody>

                        <tfoot id='08rU1'></tfoot>

                      • <small id='08rU1'></small><noframes id='08rU1'>

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

                        1. <legend id='08rU1'><style id='08rU1'><dir id='08rU1'><q id='08rU1'></q></dir></style></legend>
                            主站蜘蛛池模板: 国产精品三级视频 | 在线色网站 | 亚洲欧美一区二区三区在线 | 欧美xx孕妇| 国产又粗又猛又黄又爽的视频 | 中文字幕在线免费观看 | 日韩av在线影院 | 国产欧美在线播放 | 伊人久久av| 久草综合在线 | 欧美午夜精品久久久久免费视 | 午夜在线免费观看 | 亚洲综合视频在线 | 欧美一级特黄视频 | 国产精品久久久久久久久借妻 | 久久精品综合 | 日韩激情一区二区 | 久久精品一区二区三区四区 | 日韩精品一区在线观看 | 欧美日韩综合网 | 日韩av在线免费 | 久久九九99| 免费成人结看片 | 国产欧美日韩在线视频 | 国产中文字幕一区二区 | 97色伦图片| 在线观看a视频 | 成人av一区二区三区在线观看 | 日韩欧美中文字幕在线观看 | 99久久精品国产亚洲 | 日韩国产精品视频 | 国产欧美精品一区二区 | 亚洲第一黄色 | 亚洲一区视频在线 | 久久黄网| 国产精品99久久久久久久久久久久 | 午夜免费av | 伊人av影院 | 一区二区欧美日韩 | 国产成人精品一区二区三区在线 | 免费在线观看av |