久久久久久久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. 收到錯誤“無法從 Newtonsoft.Json.Linq.JProperty 添加或

        Getting the error quot;Cannot add or remove items from Newtonsoft.Json.Linq.JPropertyquot; in Json.net(收到錯誤“無法從 Newtonsoft.Json.Linq.JProperty 添加或刪除項目在 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>

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

                  問題描述

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

                  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

                  附加信息:無法添加或刪除項目Newtonsoft.Json.Linq.JProperty.

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

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

                  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 是一個非常大的文件,其中 docs 數(shù)組包含許多元素,如下所示(為清楚起見再次簡化):

                  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 反序列化為特定類型,但仍然忽略其他字段,那將是一個不錯的選擇.

                  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 是一個 List 和你的 MyObject 類看起來像這樣:

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

                  這是因為 Json.Net 默認(rèn)會忽略缺失的屬性.由于 MyObject 類不包含要反序列化的 _id 屬性,因此您無需費(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() 不起作用的原因說明

                  Explanation of why Remove() didn't work

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

                  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)您請求 token["_id"] 時,您會返回名為 _idJPropertyvalue>,而不是 JProperty 本身.因此,如果您嘗試對該值調(diào)用 Remove(),您將收到錯誤消息.要使其按照您的方式工作,您需要像這樣使用 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 的屬性并給我值.如果存在,獲取該值的父級(屬性),并將其從其父級(包含 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() 方法直接訪問屬性.但是,此方法僅適用于 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();
                      ...
                  }
                  

                  最后,正如評論中提到的,如果您使用的是 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)于收到錯誤“無法從 Newtonsoft.Json.Linq.JProperty 添加或刪除項目"在 Json.net 中的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

                  相關(guān)文檔推薦

                  Ignore whitespace while reading XML(讀取 XML 時忽略空格)
                  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(解析格式錯誤的 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>
                            主站蜘蛛池模板: 色综久久 | 亚洲欧美精品久久 | 精品美女视频在免费观看 | 国产美女永久免费无遮挡 | 国产又爽又黄的视频 | 久久综合888| 久久网亚洲 | 久久大香| 91一区二区| 亚洲精品久久久9婷婷中文字幕 | 成人在线观看免费观看 | 欧美另类日韩 | 国产做爰 | 久草免费在线 | jlzzjlzz国产精品久久 | 999免费视频 | 日韩在线中文 | 在线日韩中文字幕 | 久草视频在 | 一区二视频 | 日韩欧美二区 | 国产成人免费在线 | 黄色亚洲网站 | 美女视频h | 99在线免费观看视频 | 成人超碰| 欧美日韩高清 | 亚洲色图综合网 | 99精品一区二区三区 | 久久精品久久久久久 | 欧美国产精品一区二区三区 | 亚洲午夜精品一区二区三区 | 国产91在线观看 | 午夜欧美 | 欧美精品一区在线发布 | 中文字幕日韩一区 | 国产乱码精品一区二区三区五月婷 | 成人一区二区视频 | 久久久久久国产免费视网址 | 五月婷婷 六月丁香 | 日韩欧美在线观看 |