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

          <bdo id='CnGLZ'></bdo><ul id='CnGLZ'></ul>

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

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

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

        如何使用 JSON.NET 確保字符串是有效的 JSON

        How to make sure that string is valid JSON using JSON.NET(如何使用 JSON.NET 確保字符串是有效的 JSON)
        <i id='0ki8a'><tr id='0ki8a'><dt id='0ki8a'><q id='0ki8a'><span id='0ki8a'><b id='0ki8a'><form id='0ki8a'><ins id='0ki8a'></ins><ul id='0ki8a'></ul><sub id='0ki8a'></sub></form><legend id='0ki8a'></legend><bdo id='0ki8a'><pre id='0ki8a'><center id='0ki8a'></center></pre></bdo></b><th id='0ki8a'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='0ki8a'><tfoot id='0ki8a'></tfoot><dl id='0ki8a'><fieldset id='0ki8a'></fieldset></dl></div>
        • <bdo id='0ki8a'></bdo><ul id='0ki8a'></ul>

          <small id='0ki8a'></small><noframes id='0ki8a'>

                <tbody id='0ki8a'></tbody>

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

                  <tfoot id='0ki8a'></tfoot>
                  本文介紹了如何使用 JSON.NET 確保字符串是有效的 JSON的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我有一個原始字符串.我只想驗證字符串是否是有效的 JSON.我正在使用 JSON.NET.

                  I have a raw string. I just want to validate whether the string is valid JSON or not. I'm using JSON.NET.

                  推薦答案

                  通過代碼:

                  最好的辦法是在 try-catch 中使用 parse 并在解析失敗的情況下捕獲異常.(我不知道有任何 TryParse 方法).

                  Your best bet is to use parse inside a try-catch and catch exception in case of failed parsing. (I am not aware of any TryParse method).

                  (使用 JSON.Net)

                  最簡單的方法是使用 Parse 字符串JToken.Parse,并檢查字符串是否以 {[ 開頭并以 }] 分別 (從這個 回答):

                  Simplest way would be to Parse the string using JToken.Parse, and also to check if the string starts with { or [ and ends with } or ] respectively (added from this answer):

                  private static bool IsValidJson(string strInput)
                  {
                      if (string.IsNullOrWhiteSpace(strInput)) { return false;}
                      strInput = strInput.Trim();
                      if ((strInput.StartsWith("{") && strInput.EndsWith("}")) || //For object
                          (strInput.StartsWith("[") && strInput.EndsWith("]"))) //For array
                      {
                          try
                          {
                              var obj = JToken.Parse(strInput);
                              return true;
                          }
                          catch (JsonReaderException jex)
                          {
                              //Exception in parsing json
                              Console.WriteLine(jex.Message);
                              return false;
                          }
                          catch (Exception ex) //some other exception
                          {
                              Console.WriteLine(ex.ToString());
                              return false;
                          }
                      }
                      else
                      {
                          return false;
                      }
                  }
                  

                  {[ 等添加檢查的原因是基于 JToken.Parse 將解析值的事實例如 1234"'a string'" 作為有效標記.另一種選擇可能是在解析中同時使用 JObject.ParseJArray.Parse 并查看其中是否有人成功,但我相信檢查 {}[] 應該更容易. (感謝@RhinoDevel 指出)

                  The reason to add checks for { or [ etc was based on the fact that JToken.Parse would parse the values such as "1234" or "'a string'" as a valid token. The other option could be to use both JObject.Parse and JArray.Parse in parsing and see if anyone of them succeeds, but I believe checking for {} and [] should be easier. (Thanks @RhinoDevel for pointing it out)

                  沒有 JSON.Net

                  您可以使用 .Net framework 4.5 System.Json 命名空間,如:

                  You can utilize .Net framework 4.5 System.Json namespace ,like:

                  string jsonString = "someString";
                  try
                  {
                      var tmpObj = JsonValue.Parse(jsonString);
                  }
                  catch (FormatException fex)
                  {
                      //Invalid json format
                      Console.WriteLine(fex);
                  }
                  catch (Exception ex) //some other exception
                  {
                      Console.WriteLine(ex.ToString());
                  }
                  

                  (但是,您必須使用以下命令通過 Nuget 包管理器安裝 System.Json:PM>;Install-Package System.Json -Version 4.0.20126.16343在包管理器控制臺上)(取自這里)

                  (But, you have to install System.Json through Nuget package manager using command: PM> Install-Package System.Json -Version 4.0.20126.16343 on Package Manager Console) (taken from here)

                  非編碼方式:

                  通常,當有一個小的 json 字符串并且您試圖在 json 字符串中查找錯誤時,我個人更喜歡使用可用的在線工具.我通常做的是:

                  Usually, when there is a small json string and you are trying to find a mistake in the json string, then I personally prefer to use available on-line tools. What I usually do is:

                  • 將 JSON 字符串粘貼到 JSONLint The JSON Validator 并查看是否它是一個有效的 JSON.
                  • 稍后將正確的 JSON 復制到 http://json2csharp.com/ 和為它生成一個模板類,然后反序列化它使用 JSON.Net.
                  • Paste JSON string in JSONLint The JSON Validator and see if its a valid JSON.
                  • Later copy the correct JSON to http://json2csharp.com/ and generate a template class for it and then de-serialize it using JSON.Net.

                  這篇關于如何使用 JSON.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='Roj7d'><style id='Roj7d'><dir id='Roj7d'><q id='Roj7d'></q></dir></style></legend>
                      <i id='Roj7d'><tr id='Roj7d'><dt id='Roj7d'><q id='Roj7d'><span id='Roj7d'><b id='Roj7d'><form id='Roj7d'><ins id='Roj7d'></ins><ul id='Roj7d'></ul><sub id='Roj7d'></sub></form><legend id='Roj7d'></legend><bdo id='Roj7d'><pre id='Roj7d'><center id='Roj7d'></center></pre></bdo></b><th id='Roj7d'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='Roj7d'><tfoot id='Roj7d'></tfoot><dl id='Roj7d'><fieldset id='Roj7d'></fieldset></dl></div>

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

                        <tbody id='Roj7d'></tbody>

                          <tfoot id='Roj7d'></tfoot>
                            <bdo id='Roj7d'></bdo><ul id='Roj7d'></ul>
                            主站蜘蛛池模板: 91麻豆精品国产91久久久久久久久 | 欧美国产日韩一区二区三区 | 精品综合视频 | 欧美国产91 | 成人精品一区二区三区中文字幕 | 成人一级视频在线观看 | av国产精品毛片一区二区小说 | www.久久99| 国产伊人精品 | 91久久久久久久久久久 | 毛片入口 | 成人网视频 | 精品日韩一区 | 日一区二区| 亚洲国产片 | 国产成人99 | 中文字幕视频在线观看免费 | 亚洲情侣视频 | 欧美激情精品久久久久久免费 | 一区二区在线免费观看视频 | 中文在线一区二区 | 亚洲精品91 | 国产99久久久国产精品 | 欧美亚洲国语精品一区二区 | 久久y| 99福利视频 | 999视频 | 国产精品美女久久久久aⅴ国产馆 | 精精国产视频 | 日韩精品无码一区二区三区 | 精品一二三区在线观看 | 天堂网色 | 色婷婷一区二区三区四区 | 伊人精品在线视频 | 可以在线看的黄色网址 | www.亚洲 | 国产成人a亚洲精品 | 91精品一区| 成人在线电影在线观看 | 麻豆a级片 | 久久久久成人精品 |