CslJson.pas 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. //------------------------------------------------------------------------------
  2. // 类名: TCslJson
  3. //
  4. // 描述: 简单的Json字符串解析。
  5. // 为简化操作,该类对Json字符串的格式有一定的要求:
  6. // ①键名字不能重复;②键值全部是字符串。格式如下:
  7. // {"status":"true","msg":{"curr":"1","status2":"\u5ba1\u6838\u4e2d",
  8. // "name":"珠海纵横"}}
  9. //
  10. // 网上找过几个三方Json控件,基本不能用,有个叫lkJson的刚开始还不错,但
  11. // 随着需求的深入,发现问题越来越多:不能嵌套数组、不能用索引的方式遍历
  12. // 子结点(始终返回Null)、Delphi处理转义字符困难等问题。另外有些三方
  13. // Json控件(如superobject1.24)只支持Delphi XE等高版本, 表现为有些语法在
  14. // Delphi7中不支持、“Integer overflow”报错等。太多问题,无奈,自己写
  15. // 个简单的解析。有点山寨,但能解决问题。有空再写个专业的。
  16. //
  17. // 作者: Chenshilong, 2014-07-03
  18. //------------------------------------------------------------------------------
  19. unit CslJson;
  20. interface
  21. uses
  22. SysUtils, Classes, Dialogs;
  23. type
  24. TOVArr = array of array of string; // ObjectValuesArray
  25. TCslJson = class
  26. private
  27. FText: string;
  28. FOVArr: TOVArr;
  29. procedure SetText(const Value: string);
  30. function GetValue(const AField: string): string;
  31. // 处理转义字符的问题
  32. function Transfer(AStr: string): string;
  33. public
  34. property Text: string read FText write SetText;
  35. // 取指定键名字的键值。如果键值是个对象则返回{Object}
  36. // 键值带不带"号都能正确解析,一律返回字符串类型。键值为空不影响结果,
  37. // 没有类型转换、溢出等问题。
  38. property Value[const AField: string]: string read GetValue;
  39. // 如果指定键名字的键值是个对象,则把对象的每个属性值依次填入数组AArray中。
  40. // AArray数组下标任意,数组元素个数任意(不会溢出)
  41. procedure ChildValues(AField: string; var AArray: array of string);
  42. function ArrayText(AField: string): string; // Json二维数组对象序列化后的字符串值
  43. function ArrayElementCount(AField: string): Integer; // Json二维数组对象中元素个数
  44. function ArrayElementText(AField: string; AIndex: Integer): string; // Json二维数组的第AIndex个元素的字符串值
  45. function ArrayElementPropertyCount(AElementText: string): Integer; // Json二维数组的元素的键值对个数
  46. function ArrayValues(AField: string): TOVArr;
  47. end;
  48. implementation
  49. { TcslJson }
  50. procedure TCslJson.SetText(const Value: string);
  51. begin
  52. FText := Value;
  53. end;
  54. function TCslJson.GetValue(const AField: string): string;
  55. var sF: string;
  56. iPos, iCur: Integer;
  57. begin
  58. Result := '';
  59. if AField = '' then Exit;
  60. sF := Format('"%s":', [AField]);
  61. iPos := POS(sF, FText);
  62. if iPos = 0 then Exit;
  63. iCur := iPos + Length(sF);
  64. iPos := iCur;
  65. while (FText[iCur] <> ',') and (FText[iCur] <> '}') do
  66. Inc(iCur);
  67. Result := Copy(FText, iPos, iCur - iPos);
  68. if Pos('{', Result) > 0 then
  69. begin
  70. Result := '{Object}';
  71. Exit;
  72. end;
  73. Result := StringReplace(Result, '"', '', [rfReplaceAll]);
  74. Result := Transfer(Result);
  75. end;
  76. procedure TCslJson.ChildValues(AField: string; var AArray: array of string);
  77. var sField, sText, sValue: string;
  78. iPos, iCur, i, j: Integer;
  79. vSL: TStringList;
  80. begin
  81. if AField = '' then Exit;
  82. sField := Format('"%s":', [AField]);
  83. iPos := POS(sField, FText);
  84. if iPos = 0 then Exit;
  85. iCur := iPos + Length(sField);
  86. iPos := iCur;
  87. while (FText[iCur] <> '}') do
  88. Inc(iCur);
  89. sText := Copy(FText, iPos, iCur - iPos);
  90. sText := StringReplace(sText, '{', '', []); // 去掉第一个"{"
  91. if Pos('{', sText) > 0 then // 证明还嵌套有子对象,不支持
  92. begin
  93. AArray[Low(AArray)] := '{Contain Sub Object}';
  94. Exit;
  95. end;
  96. // "curr":"1","status2":"\u5ba1\u6838\u4e2d","total":"1","name":"珠海纵横"
  97. vSL := TStringList.Create;
  98. try
  99. while Length(sText) > 0 do
  100. begin
  101. iPos := POS(':"', sText);
  102. iCur := iPos + 2;
  103. iPos := iCur;
  104. while (sText[iCur] <> '"') do
  105. Inc(iCur);
  106. sValue := Copy(sText, iPos, iCur - iPos);
  107. sValue := Transfer(sValue);
  108. vSL.Add(sValue);
  109. iCur := iCur + 2;
  110. sText := Copy(sText, iCur, Length(sText) - iCur + 1);
  111. end;
  112. // 这种方式保证数组下标任意,数组元素个数任意,不受限制
  113. j := 1;
  114. for i := Low(AArray) to High(AArray) do
  115. begin
  116. if J > vSL.Count then Break;
  117. AArray[i] := vSL[j - 1];
  118. Inc(j);
  119. end;
  120. finally
  121. vSL.Free;
  122. end;
  123. end;
  124. function TCslJson.Transfer(AStr: string): string;
  125. begin
  126. AStr := StringReplace(AStr, '\/', '\', [rfReplaceAll]); // 路径中的“\/”
  127. Result := AStr;
  128. end;
  129. function TCslJson.ArrayText(AField: string): string;
  130. var sField: string;
  131. iPos, iCur: Integer;
  132. begin
  133. Result := '';
  134. if AField = '' then Exit;
  135. sField := Format('"%s":[', [AField]);
  136. iPos := POS(sField, FText);
  137. if iPos = 0 then Exit;
  138. iCur := iPos + Length(sField);
  139. iPos := iCur;
  140. while (FText[iCur] <> ']') do
  141. Inc(iCur);
  142. Result := Copy(FText, iPos, iCur - iPos);
  143. end;
  144. function TCslJson.ArrayElementText(AField: string; AIndex: Integer): string;
  145. var sAT: string;
  146. iLen, i, iCount, iPos1, iPos2: Integer;
  147. begin
  148. Result := '';
  149. iCount := 0;
  150. iPos1 := 0;
  151. iPos2 := 0;
  152. sAT := ArrayText(AField);
  153. iLen := Length(sAT);
  154. for i := 1 to iLen do
  155. begin
  156. if sAT[i] = '{' then
  157. begin
  158. Inc(iCount);
  159. if iCount = AIndex then
  160. begin
  161. iPos1 := i;
  162. Break;
  163. end;
  164. end;
  165. end;
  166. for i := iPos1 to iLen do
  167. begin
  168. if sAT[i] = '}' then
  169. begin
  170. iPos2 := i;
  171. Break;
  172. end;
  173. end;
  174. Result := Copy(sAT, iPos1, iPos2 - iPos1 + 1);
  175. end;
  176. function TCslJson.ArrayElementCount(AField: string): Integer;
  177. var i, iLen: Integer;
  178. sAText: string;
  179. begin
  180. Result := 0;
  181. sAText := ArrayText(AField);
  182. iLen := Length(sAText);
  183. for i := 1 to iLen do
  184. begin
  185. if sAText[i] = '}' then
  186. Inc(Result);
  187. end;
  188. end;
  189. function TCslJson.ArrayElementPropertyCount(AElementText: string): Integer;
  190. var i, iLen: Integer;
  191. begin
  192. Result := 1;
  193. iLen := Length(AElementText);
  194. for i := 1 to iLen do
  195. begin
  196. if AElementText[i] = ',' then
  197. if (i < iLen) and (AElementText[i + 1] = '"') then
  198. Inc(Result);
  199. end;
  200. end;
  201. function TCslJson.ArrayValues(AField: string): TOVArr;
  202. var sText: string;
  203. iPos, iCur, i, j, iECount, iPCount: Integer;
  204. vSL: TStringList;
  205. begin
  206. Result := nil;
  207. if AField = '' then Exit;
  208. iECount := ArrayElementCount(AField);
  209. if iECount = 0 then Exit;
  210. iPCount := ArrayElementPropertyCount(ArrayElementText(AField, 1));
  211. SetLength(FOVArr, iECount, iPCount);
  212. vSL := TStringList.Create;
  213. try
  214. for i := 1 to iECount do
  215. begin
  216. sText := ArrayElementText(AField, i);
  217. vSL.Clear;
  218. while Length(sText) > 0 do
  219. begin
  220. iPos := POS(':"', sText);
  221. iCur := iPos + 2;
  222. iPos := iCur;
  223. while (sText[iCur] <> '"') do
  224. Inc(iCur);
  225. vSL.Add(Transfer(Copy(sText, iPos, iCur - iPos)));
  226. iCur := iCur + 2;
  227. sText := Copy(sText, iCur, Length(sText) - iCur + 1);
  228. end;
  229. for j := 0 to vSL.Count - 1 do
  230. begin
  231. FOVArr[i - 1, j] := vSL[j];
  232. end;
  233. end;
  234. finally
  235. vSL.Free;
  236. end;
  237. Result := FOVArr;
  238. end;
  239. end.