-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuIterateJSON.pas
174 lines (157 loc) · 4.41 KB
/
uIterateJSON.pas
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
unit uIterateJSON;
interface
uses
JSON, IOUtils, VCL.StdCtrls, System.Classes;
type
TIterateJSON = class
class procedure Iterate;
private
class function CreateJson: TJSONObject;
class procedure GetJsonArr(jsonArr: TJSONArray);
class function GetKeyValue(JsonObj: TJSONObject): TJSONObject;
class procedure CreateJSONValueArray(o: TJSONObject; arr: TJSONArray);
class function CreateArrayElem(Strings: TStrings): TJSONObject;
end;
implementation
uses
System.SysUtils;
class procedure TIterateJSON.Iterate;
var
JSONObject : TJSONObject;
jstr: string;
begin
jstr :=
'{' +
' "DataList": [' +
' {' +
' "Period": "201911",' +
' "CODE": "3",' +
' "SCR_ENG": "Parsed from string",' +
' "CODE1": "101",' +
' "SCR_ENG1": "Film, entertainment",' +
' "CODE2": null,' +
' "DTVAL_CO": "9222"' +
' },' +
' {' +
' "Period": "202004",' +
' "CODE": "4",' +
' "SCR_ENG": "Same as Code 3",' +
' "CODE1": "101",' +
' "SCR_ENG1": "Agriculture, foresty, hunting and fishery",' +
' "CODE2": null,' +
' "DTVAL_CO": "1686"' +
' }' +
' ],' +
' "Response": "success"' +
'}';
JSONObject := TJSONObject.Create;
try
if FileExists('json.txt') then
//Creating object from file
JSONObject := TJSONObject.ParseJSONValue(TFile.ReadAllBytes('json.txt'), 0) as TJSONObject
else
//Creating object from text
JSONObject := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(jstr), 0) as TJSONObject;
GetKeyValue(JSONObject);
finally
JSONObject.Free;
end;
ReadLn;
WriteLn('');
WriteLn('Create and print JSON:');
WriteLn('');
GetKeyValue(CreateJSON);
end;
class function TIterateJSON.CreateJson: TJSONObject;
var
jsonValue: TJSONValue;
jsonArray: TJSONArray;
jsonPair: TJSONPair;
begin
Result := TJSONObject.Create;
Result.Format(2);
jsonArray := TJSONArray.Create;
jsonPair := TJSONPair.Create('DataList', jsonArray);
CreateJSONValueArray(Result, jsonArray);
Result.AddPair(jsonPair);
end;
class procedure TIterateJSON.CreateJSONValueArray(o: TJSONObject; arr: TJSONArray);
var
Strings: TStrings;
begin
Strings := TStringList.Create;
try
Strings.AddPair('Period', '201703');
Strings.AddPair('CODE', '1');
Strings.AddPair('SCR_ENG', 'Active');
Strings.AddPair('CODE1', '101');
Strings.AddPair('SCR_ENG1', 'Agriculture, foresty, hunting and fishery');
Strings.AddPair('CODE2', '');
Strings.AddPair('DTVAL_CO', '3802');
arr.AddElement(CreateArrayElem(Strings) as TJSONObject);
Strings.Clear;
Strings.AddPair('Period', '201703');
Strings.AddPair('CODE', '2');
Strings.AddPair('SCR_ENG', 'have not started activities');
Strings.AddPair('CODE1', '101');
Strings.AddPair('SCR_ENG1', 'Agriculture, foresty, hunting and fishery');
Strings.AddPair('CODE2', '');
Strings.AddPair('DTVAL_CO', '1686');
arr.AddElement(CreateArrayElem(Strings) as TJSONObject);
finally
Strings.Free;
end;
end;
class function TIterateJSON.CreateArrayElem(Strings: TStrings): TJSONObject;
var
I: Integer;
key: string;
begin
Result := TJSONObject.Create;
for I := 0 to Strings.Count - 1 do
begin
key := Strings.KeyNames[I];
Result.AddPair(key, Strings.Values[key]);
end;
end;
class function TIterateJSON.GetKeyValue(JsonObj: TJSONObject): TJSONObject;
var
I: Integer;
jsonStr: string;
key: string;
value: string;
begin
for I := 0 to JsonObj.Count - 1 do
begin
key := JsonObj.Pairs[I].JSONString.Value;
value := JsonObj.GetValue(key).Value;
if value = '' then
begin
Write('Key: ' + key);
jsonStr := JsonObj.GetValue(key).ToJSON.TrimLeft;
if jsonStr.StartsWith('{') then
GetKeyValue(JsonObj.GetValue(key) as TJSONObject)
else
if jsonStr.StartsWith('[') then
GetJsonArr(JsonObj.GetValue(key) as TJSONArray);
end else
WriteLn('Key: ' + key + ', Value: ' + value);
end;
end;
class procedure TIterateJSON.GetJsonArr(jsonArr: TJSONArray);
var
J: Integer;
begin
WriteLn('');
for J := 0 to jsonArr.Count - 1 do
begin
WriteLn('[');
if jsonArr.Get(0).ToJSON.TrimLeft.StartsWith('{') then
GetKeyValue(jsonArr.Get(0) as TJSONObject)
else
if jsonArr.Get(0).ToJSON.TrimLeft.StartsWith('[') then
GetJsonArr(jsonArr.Get(0) as TJSONArray);
WriteLn(']');
end;
end;
end.