資源簡介
c#版本的校驗文本是否為合法json格式,單獨一個類無需其他引用

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.Linq;
using?System.Text;
namespace?CheckJSON
{
????internal?class?JsonSplit
????{
????????private?static?bool?IsJsonstart(ref?string?json)
????????{
????????????if?(!string.IsNullOrEmpty(json))
????????????{
????????????????json?=?json.Trim(‘\r‘?‘\n‘?‘?‘);
????????????????if?(json.Length?>?1)
????????????????{
????????????????????char?s?=?json[0];
????????????????????char?e?=?json[json.Length?-?1];
????????????????????return?(s?==?‘{‘?&&?e?==?‘}‘)?||?(s?==?‘[‘?&&?e?==?‘]‘);
????????????????}
????????????}
????????????return?false;
????????}
????????internal?static?bool?IsJson(string?json)
????????{
????????????int?errIndex;
????????????return?IsJson(json?out?errIndex);
????????}
????????internal?static?bool?IsJson(string?json?out?int?errIndex)
????????{
????????????errIndex?=?0;
????????????if?(IsJsonstart(ref?json))
????????????{
????????????????CharState?cs?=?new?CharState();
????????????????char?c;
????????????????for?(int?i?=?0;?i?????????????????{
????????????????????c?=?json[i];
????????????????????if?(SetCharState(c?ref?cs)?&&?cs.childrenStart)//設置關鍵符號狀態。
????????????????????{
????????????????????????string?item?=?json.Substring(i);
????????????????????????int?err;
????????????????????????int?length?=?GetValueLength(item?true?out?err);
????????????????????????cs.childrenStart?=?false;
????????????????????????if?(err?>?0)
????????????????????????{
????????????????????????????errIndex?=?i?+?err;
????????????????????????????return?false;
????????????????????????}
????????????????????????i?=?i?+?length?-?1;
????????????????????}
????????????????????if?(cs.isError)
????????????????????{
????????????????????????errIndex?=?i;
????????????????????????return?false;
????????????????????}
????????????????}
????????????????return?!cs.arrayStart?&&?!cs.jsonstart;
????????????}
????????????return?false;
????????}
????????///?
????????///?獲取值的長度(當Json值嵌套以“{“或“[“開頭時)
????????///?
????????private?static?int?GetValueLength(string?json?bool?breakOnErr?out?int?errIndex)
????????{
????????????errIndex?=?0;
????????????int?len?=?0;
????????????if?(!string.IsNullOrEmpty(json))
????????????{
????????????????CharState?cs?=?new?CharState();
????????????????char?c;
????????????????for?(int?i?=?0;?i?????????????????{
????????????????????c?=?json[i];
????????????????????if?(!SetCharState(c?ref?cs))//設置關鍵符號狀態。
????????????????????{
????????????????????????if?(!cs.jsonstart?&&?!cs.arrayStart)//json結束,又不是數組,則退出。
????????????????????????{
????????????????????????????break;
????????????????????????}
????????????????????}
????????????????????else?if?(cs.childrenStart)//正常字符,值狀態下。
????????????????????{
????????????????????????int?length?=?GetValueLength(json.Substring(i)?breakOnErr?out?errIndex);//遞歸子值,返回一個長度。。。
????????????????????????cs.childrenStart?=?fa
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????12288??2015-08-28?16:11??CheckJSON\CheckJSON\bin\Debug\CheckJSON.exe
?????文件??????32256??2015-08-28?16:11??CheckJSON\CheckJSON\bin\Debug\CheckJSON.pdb
?????文件??????22472??2015-08-31?08:53??CheckJSON\CheckJSON\bin\Debug\CheckJSON.vshost.exe
?????文件????????490??2012-06-06?02:06??CheckJSON\CheckJSON\bin\Debug\CheckJSON.vshost.exe.manifest
?????文件???????3768??2015-08-28?14:12??CheckJSON\CheckJSON\CheckJSON.csproj
?????文件??????16252??2015-08-28?14:12??CheckJSON\CheckJSON\Class1.cs
?????文件???????1107??2015-08-28?16:11??CheckJSON\CheckJSON\Form1.cs
?????文件???????2022??2015-08-28?14:12??CheckJSON\CheckJSON\Form1.Designer.cs
?????文件???????5817??2015-08-28?14:12??CheckJSON\CheckJSON\Form1.resx
?????文件????????882??2015-08-31?08:53??CheckJSON\CheckJSON\obj\Debug\CheckJSON.csproj.FileListAbsolute.txt
?????文件????????975??2015-08-28?16:10??CheckJSON\CheckJSON\obj\Debug\CheckJSON.csproj.GenerateResource.Cache
?????文件???????2209??2015-08-28?16:10??CheckJSON\CheckJSON\obj\Debug\CheckJSON.csprojResolveAssemblyReference.cache
?????文件??????12288??2015-08-28?16:11??CheckJSON\CheckJSON\obj\Debug\CheckJSON.exe
?????文件????????180??2015-08-28?16:10??CheckJSON\CheckJSON\obj\Debug\CheckJSON.Form1.resources
?????文件??????32256??2015-08-28?16:11??CheckJSON\CheckJSON\obj\Debug\CheckJSON.pdb
?????文件????????180??2015-08-28?16:10??CheckJSON\CheckJSON\obj\Debug\CheckJSON.Properties.Resources.resources
?????文件???????1451??2015-08-31?08:53??CheckJSON\CheckJSON\obj\Debug\DesignTimeResolveAssemblyReferences.cache
?????文件???????6905??2015-08-28?14:11??CheckJSON\CheckJSON\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????文件????????490??2015-08-28?14:05??CheckJSON\CheckJSON\Program.cs
?????文件???????1338??2015-08-28?14:05??CheckJSON\CheckJSON\Properties\AssemblyInfo.cs
?????文件???????2870??2015-08-28?14:05??CheckJSON\CheckJSON\Properties\Resources.Designer.cs
?????文件???????5612??2015-08-28?14:05??CheckJSON\CheckJSON\Properties\Resources.resx
?????文件???????1096??2015-08-28?14:05??CheckJSON\CheckJSON\Properties\Settings.Designer.cs
?????文件????????249??2015-08-28?14:05??CheckJSON\CheckJSON\Properties\Settings.settings
?????文件????????917??2015-08-28?14:05??CheckJSON\CheckJSON.sln
????..A..H.????331264??2015-08-31?08:58??CheckJSON\CheckJSON.v11.suo
?????目錄??????????0??2015-08-28?14:05??CheckJSON\CheckJSON\obj\Debug\TempPE
?????目錄??????????0??2015-08-28?16:10??CheckJSON\CheckJSON\bin\Debug
?????目錄??????????0??2015-08-31?08:53??CheckJSON\CheckJSON\obj\Debug
?????目錄??????????0??2015-08-28?14:05??CheckJSON\CheckJSON\bin
............此處省略7個文件信息
- 上一篇:杜洋入門C#設計視頻資料
- 下一篇:C#訪問共享文件夾
評論
共有 條評論