mORMot操作JSON

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
type
  TCusObject = class
  private
    FB: string;
    FA: string;
    procedure SetA(const Value: string);
    procedure SetB(const Value: string);
  published
    property A: string read FA write SetA;
    property B: string read FB write SetB;
  end;

procedure TCusObject.SetA(const Value: string);
begin
  FA := Value;
end;

procedure TCusObject.SetB(const Value: string);
begin
  FB := Value;
end;


uses
  SynCommons;
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
procedure TForm1.btn1Click(Sender: TObject);
var
  json,json1:Variant;
begin
  json := _JsonFast(mmo1.Lines.Text); // more faster O(∩_∩)O
  json1 := _Json(mmo1.Lines.Text);
  mmo2.Lines.Add('==========Parse Json String To Variant==========');
  mmo2.Lines.Add('a:' + json.a);
  mmo2.Lines.Add('b:' + json.b);
  mmo2.Lines.Add('==========END==========');

end;

procedure TForm1.btn2Click(Sender: TObject);
var
  json:Variant;
begin
  json := _JsonFast(mmo1.Lines.Text); // more faster O(∩_∩)O

  json.c := 'This is C';

  mmo2.Lines.Add('==========Add Field To Json==========');
  mmo2.Lines.Add('New Json:' + VariantToString(json));
  mmo2.Lines.Add('a:' + json.a);
  mmo2.Lines.Add('b:' + json.b);
  mmo2.Lines.Add('c:' + json.c);
  mmo2.Lines.Add('==========END==========');

end;

procedure TForm1.btn3Click(Sender: TObject);
var
  json:Variant;
begin
  json := _JsonFast(mmo1.Lines.Text); // more faster O(∩_∩)O

  TDocVariantData(json).Delete('a');

  mmo2.Lines.Add('==========Remove Field From Json==========');
  mmo2.Lines.Add('New Json:' + VariantToString(json));
  mmo2.Lines.Add('==========END==========');

end;

procedure TForm1.btn4Click(Sender: TObject);
var
  json,json1:Variant;
begin
  json := _JsonFast(mmo1.Lines.Text); // more faster O(∩_∩)O
  json1 :=  _JsonFast('{}');
  json1.c := 'This is C';
  json1.d := 'This is D';
  TDocVariantData(json).AddValue('c',VariantToUTF8(json1));

  mmo2.Lines.Add('==========Add Json To Json As One Field==========');
  mmo2.Lines.Add('New Json:' + VariantToString(json));
  mmo2.Lines.Add('==========END==========');

end;

procedure TForm1.btn5Click(Sender: TObject);
var
  json,json1:Variant;
  prettyJson:RawJSON;
begin
  json := _JsonFast(mmo1.Lines.Text); // more faster O(∩_∩)O
  json1 :=  _JsonFast('{}');
  json1.c := 'This is C';
  json1.d := 'This is D';
  TDocVariantData(json).AddValue('c',VariantToUTF8(json1));
  prettyJson := TDocVariantData(json).ToJSON('','',jsonHumanReadable);
  mmo2.Lines.Add('==========Add Json To Json As One Field==========');
  mmo2.Lines.Add('New Json:' +prettyJson);
  mmo2.Lines.Add('==========END==========');
end;

procedure TForm1.btn6Click(Sender: TObject);
var
  json:Variant;
  prettyJson:RawJSON;
begin
  json := _JsonFast(mmo1.Lines.Text); // more faster O(∩_∩)O

  TDocVariantData(json).AddOrUpdateValue('c',_Json( '{"name":"This is C name","sex":"This is C sex"}'));


  mmo2.Lines.Add('==========Add Sub Object==========');
  prettyJson := TDocVariantData(json).ToJSON('','',jsonHumanReadable);
  mmo2.Lines.Add('New Json:' + prettyJson);
  mmo2.Lines.Add('a:' + json.a);
  mmo2.Lines.Add('b:' + json.b);
  mmo2.Lines.Add('c.name:' + json.c.name);
  mmo2.Lines.Add('==========END==========');

end;
记录平时瞎折腾遇到的各种问题, 方便查找
使用 Hugo 构建
主题 Stack 3.29.0Jimmy 设计