forked from Villavu/Simba
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjson_read.simba
More file actions
43 lines (37 loc) · 1.18 KB
/
json_read.simba
File metadata and controls
43 lines (37 loc) · 1.18 KB
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
const
SOME_JSON =
'{ "postOfficeBox": "123", ' + LINE_SEP +
' "streetAddress": "456 Main St", ' + LINE_SEP +
' "region": "State", ' + LINE_SEP +
' "postalCode": 12345, ' + LINE_SEP +
' "open": true ' + LINE_SEP +
'}';
var
json: TJSONItem;
I: Integer;
street: String;
postal: Int64;
begin
json := ParseJSON(SOME_JSON);
// if you the structure ...
if json.GetString('streetAddress', street) then
WriteLn(street);
if json.GetInt('postalCode', postal) then
WriteLn(postal);
WriteLn(json.Item['streetAddress'].AsString);
WriteLn(json.Item['postalCode'].AsInt);
// or loop over the items
for I := 0 to json.Count - 1 do
begin
WriteLn('Item[', I, ']');
WriteLn(' Key = ', json.key[I]);
WriteLn(' Typ = ', json.Item[I].Typ);
case json.Item[i].Typ of
EJSONType.INT: WriteLn(' Value = ', json.Item[i].AsInt);
EJSONType.FLOAT: WriteLn(' Value = ', json.Item[i].AsFloat);
EJSONType.BOOL: WriteLn(' Value = ', json.Item[i].AsBool);
EJSONType.STR: WriteLn(' Value = ', json.Item[i].AsString);
end;
end;
json.Free();
end;