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

    <tfoot id='uJIFd'></tfoot>
  • <legend id='uJIFd'><style id='uJIFd'><dir id='uJIFd'><q id='uJIFd'></q></dir></style></legend>

      • <bdo id='uJIFd'></bdo><ul id='uJIFd'></ul>

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

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

        在 .NET 中解析大型 JSON 文件

        Parsing large JSON file in .NET(在 .NET 中解析大型 JSON 文件)
        <i id='Vqgsb'><tr id='Vqgsb'><dt id='Vqgsb'><q id='Vqgsb'><span id='Vqgsb'><b id='Vqgsb'><form id='Vqgsb'><ins id='Vqgsb'></ins><ul id='Vqgsb'></ul><sub id='Vqgsb'></sub></form><legend id='Vqgsb'></legend><bdo id='Vqgsb'><pre id='Vqgsb'><center id='Vqgsb'></center></pre></bdo></b><th id='Vqgsb'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='Vqgsb'><tfoot id='Vqgsb'></tfoot><dl id='Vqgsb'><fieldset id='Vqgsb'></fieldset></dl></div>
          <tbody id='Vqgsb'></tbody>
        • <bdo id='Vqgsb'></bdo><ul id='Vqgsb'></ul>

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

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

                1. 本文介紹了在 .NET 中解析大型 JSON 文件的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  到目前為止,我已經使用了 Json.NET 的JsonConvert.Deserialize(json)"方法,效果很好,老實說,我不需要更多的東西.

                  I have used the "JsonConvert.Deserialize(json)" method of Json.NET so far which worked quite well and to be honest, I didn't need anything more than this.

                  我正在開發一個后臺(控制臺)應用程序,該應用程序不斷從不同的 URL 下載 JSON 內容,然后將結果反序列化為 .NET 對象列表.

                  I am working on a background (console) application which constantly downloads the JSON content from different URLs, then deserializes the result into a list of .NET objects.

                   using (WebClient client = new WebClient())
                   {
                        string json = client.DownloadString(stringUrl);
                  
                        var result = JsonConvert.DeserializeObject<List<Contact>>(json);
                  
                   }
                  

                  上面的簡單代碼片段可能看起來并不完美,但它確實可以完成工作.當文件很大(15,000 個聯系人 - 48 MB 文件)時,JsonConvert.DeserializeObject 不是解決方案,并且該行會引發 JsonReaderException 異常類型.

                  The simple code snippet above doesn't probably seem perfect, but it does the job. When the file is large (15,000 contacts - 48 MB file), JsonConvert.DeserializeObject isn't the solution and the line throws an exception type of JsonReaderException.

                  下載的 JSON 內容是一個數組,這就是示例的樣子.Contact 是反序列化 JSON 對象的容器類.

                  The downloaded JSON content is an array and this is how a sample looks like. Contact is a container class for the deserialized JSON object.

                  [
                    {
                      "firstname": "sometext",
                      "lastname": "sometext"
                    },
                    {
                      "firstname": "sometext",
                      "lastname": "sometext"
                    },
                    {
                      "firstname": "sometext",
                      "lastname": "sometext"
                    },
                    {
                      "firstname": "sometext",
                      "lastname": "sometext"
                    }
                  ]
                  

                  我最初的猜測是內存不足.只是出于好奇,我嘗試將其解析為 JArray,這也導致了同樣的異常.

                  My initial guess is it runs out of memory. Just out of curiosity, I tried to parse it as JArray which caused the same exception too.

                  我已經開始深入研究 Json.NET 文檔并閱讀類似的主題.由于我還沒有設法產生一個可行的解決方案,我決定在這里發布一個問題.

                  I have started to dive into Json.NET documentation and read similar threads. As I haven't managed to produce a working solution yet, I decided to post a question here.

                  更新:在逐行反序列化時,我得到了同樣的錯誤:[.Path '', line 600003, position 1."所以下載了其中兩個并在記事本++中檢查了它們.我注意到如果數組長度超過 12,000,則在第 12000 個元素之后,["關閉,另一個數組開始.換句話說,JSON 看起來就像這樣:

                  UPDATE: While deserializing line by line, I got the same error: " [. Path '', line 600003, position 1." So downloaded two of them and checked them in Notepad++. I noticed that if the array length is more than 12,000, after 12000th element, the "[" is closed and another array starts. In other words, the JSON looks exactly like this:

                  [
                    {
                      "firstname": "sometext",
                      "lastname": "sometext"
                    },
                    {
                      "firstname": "sometext",
                      "lastname": "sometext"
                    },
                    {
                      "firstname": "sometext",
                      "lastname": "sometext"
                    },
                    {
                      "firstname": "sometext",
                      "lastname": "sometext"
                    }
                  ]
                  [
                    {
                      "firstname": "sometext",
                      "lastname": "sometext"
                    },
                    {
                      "firstname": "sometext",
                      "lastname": "sometext"
                    },
                    {
                      "firstname": "sometext",
                      "lastname": "sometext"
                    },
                    {
                      "firstname": "sometext",
                      "lastname": "sometext"
                    }
                  ]
                  

                  推薦答案

                  正如您在更新中正確診斷的那樣,問題是 JSON 有一個結束 ] 緊跟一個開始 [ 開始下一組.這種格式在整體上會使 JSON 無效,這就是 Json.NET 拋出錯誤的原因.

                  As you've correctly diagnosed in your update, the issue is that the JSON has a closing ] followed immediately by an opening [ to start the next set. This format makes the JSON invalid when taken as a whole, and that is why Json.NET throws an error.

                  幸運的是,這個問題似乎經常出現,以至于 Json.NET 實際上有一個特殊的設置來處理它.如果直接使用 JsonTextReader 讀取 JSON,可以將 SupportMultipleContent 標志設置為 true,然后使用循環反序列化每個項個人.

                  Fortunately, this problem seems to come up often enough that Json.NET actually has a special setting to deal with it. If you use a JsonTextReader directly to read the JSON, you can set the SupportMultipleContent flag to true, and then use a loop to deserialize each item individually.

                  這應該允許您以高效的內存方式成功處理非標準 JSON,而不管有多少數組或每個數組中有多少項.

                  This should allow you to process the non-standard JSON successfully and in a memory efficient manner, regardless of how many arrays there are or how many items in each array.

                      using (WebClient client = new WebClient())
                      using (Stream stream = client.OpenRead(stringUrl))
                      using (StreamReader streamReader = new StreamReader(stream))
                      using (JsonTextReader reader = new JsonTextReader(streamReader))
                      {
                          reader.SupportMultipleContent = true;
                  
                          var serializer = new JsonSerializer();
                          while (reader.Read())
                          {
                              if (reader.TokenType == JsonToken.StartObject)
                              {
                                  Contact c = serializer.Deserialize<Contact>(reader);
                                  Console.WriteLine(c.FirstName + " " + c.LastName);
                              }
                          }
                      }
                  

                  完整演示:https://dotnetfiddle.net/2TQa8p

                  這篇關于在 .NET 中解析大型 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)
                  <legend id='S1IT2'><style id='S1IT2'><dir id='S1IT2'><q id='S1IT2'></q></dir></style></legend>
                    <bdo id='S1IT2'></bdo><ul id='S1IT2'></ul>

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

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

                            主站蜘蛛池模板: 毛片一区 | 亚洲一区视频在线 | 日韩欧美不卡 | 亚洲成人高清 | 精精精精xxxx免费视频 | 久久久久亚洲精品 | 国产成人小视频 | 久久精品网 | av香蕉 | 久久国产麻豆 | 久久久精品网站 | 狠狠干夜夜草 | 久久久毛片| 欧美第一页 | 97日日碰人人模人人澡分享吧 | 欧美激情一区二区三区 | 日本色高清 | 午夜影院网站 | 日韩成人精品视频 | 亚洲国产精品成人久久久 | 欧美jizzhd精品欧美巨大免费 | 国产精品一区二 | 久久久国产一区二区三区 | 毛片综合 | 亚洲视频区 | 国产精品久久久久久久久图文区 | 国产精品99久久久久久久vr | 亚洲视频一区二区三区 | 国产精品一区久久久久 | 精品一区二区在线视频 | 日韩欧美国产精品一区二区三区 | 亚洲一区二区三区四区五区午夜 | 在线亚洲欧美 | 日韩中文字幕 | 成人在线精品视频 | 蜜桃av鲁一鲁一鲁一鲁 | 精品一区二区三区四区视频 | 青青草av网站 | 男女羞羞免费网站 | 欧美日韩国产高清 | 91久久|