FormMainUnit.pas 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. unit FormMainUnit;
  2. interface
  3. uses
  4. rjson,
  5. // System.JSON,
  6. // System.rtti,
  7. system.StrUtils,
  8. system.SysUtils, system.Types, system.UITypes, system.Classes, system.Variants,
  9. FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Memo.Types,
  10. FMX.StdCtrls, FMX.Controls.Presentation, FMX.ScrollBox, FMX.Memo, FMX.Ani;
  11. type
  12. TFormMain = class(TForm)
  13. Memo1: TMemo;
  14. btnTest: TButton;
  15. btnOpen: TButton;
  16. dlgOpen: TOpenDialog;
  17. procedure btnTestClick(Sender: TObject);
  18. procedure btnOpenClick(Sender: TObject);
  19. private
  20. public
  21. { Public declarations }
  22. end;
  23. var
  24. FormMain: TFormMain;
  25. implementation
  26. {$R *.fmx}
  27. procedure TFormMain.btnTestClick(Sender: TObject);
  28. var
  29. RJ, RJ1: TRJSON;
  30. fTemp: Extended;
  31. begin
  32. RJ['title'] := 'hello world! 你好,世界!';
  33. RJ['a.num'] := 1;
  34. RJ['a.hah'] := false;
  35. RJ['b[2]'] := 505;
  36. RJ['b[0]'] := 'first';
  37. RJ['good'] := True;
  38. RJ1 := RJ['c'];
  39. RJ1['c1'] := 1.1;
  40. RJ1['c2[2]'] := 2.33;
  41. with RJ['x'] do
  42. begin
  43. items[1] := 100;
  44. items[2] := '202';
  45. end;
  46. with RJ['y'] do
  47. begin
  48. items['ya'] := 'y1';
  49. items['yb'] := -2;;
  50. end;
  51. Memo1.Text := RJ.Format;
  52. Memo1.Lines.Add('-----------------------------------------------------------');
  53. fTemp := RJ['c.c2[3]'];
  54. Memo1.Lines.Add('fTemp:' + fTemp.ToString);
  55. fTemp := RJ['c.c3[3]'].ToFloat(-100);
  56. Memo1.Lines.Add('fTemp:' + fTemp.ToString);
  57. Memo1.Lines.Add(RJ['a.num'].ToStr('a.num not exist'));
  58. Memo1.Lines.Add(RJ['none'].ToFloat(-1).ToString);
  59. Memo1.Lines.Add(RJ['none.a3'].ToStr('none.a3 not exist'));
  60. RJ.SaveToFile('test.json', 0);
  61. end;
  62. procedure TFormMain.btnOpenClick(Sender: TObject);
  63. var
  64. RJ: TRJSON;
  65. strTmp: string;
  66. begin
  67. RJ.LoadFromFile('test.json');
  68. Memo1.Text := RJ.Format(8);
  69. Memo1.Lines.Add('-----------------------ROOT--------------------------');
  70. for var item in RJ do
  71. begin
  72. strTmp := Format('Index: %d Key: %s Value: %s',
  73. [item.Index, item.Key, item.Format(0)]);
  74. Memo1.Lines.Add(strTmp);
  75. end;
  76. Memo1.Lines.Add('-----------------------RJ[''a'']--------------------------');
  77. for var item in RJ['a'] do
  78. begin
  79. strTmp := Format('Index: %d Key: %s Value: %s',
  80. [item.Index, item.Key, item.Format(0)]);
  81. Memo1.Lines.Add(strTmp);
  82. end;
  83. Memo1.Lines.Add('-----------------------RJ[''b'']--------------------------');
  84. for var item in RJ['b'] do
  85. begin
  86. strTmp := Format('Index: %d Key: %s Value: %s',
  87. [item.Index, item.Key, item.Format(0)]);
  88. Memo1.Lines.Add(strTmp);
  89. end;
  90. Memo1.Lines.Add('-----------------------RJ[''c'']--------------------------');
  91. for var i := 0 to RJ['c'].Count - 1 do
  92. begin
  93. strTmp := Format('Index: %d Key: %s Value: %s',
  94. [RJ['c'].Pairs[i].Index, RJ['c'].Pairs[i].Key, RJ['c'].Pairs[i].Format(0)]);
  95. Memo1.Lines.Add(strTmp);
  96. end;
  97. end;
  98. end.