uses
Classes, SysUtils, fpjson, jsonparser;
procedure ParseZonesJSON(const AJsonString: string);
var
JSONData: TJSONData;
ZonesArray: TJSONArray;
ZoneObject, RegionObject: TJSONObject;
ZoneItem, RegionItem: TJSONEnum;
ZoneId, ZoneName, RegionId, RegionName, ZoneDesc, ZoneRegionId: string;
begin
try
//JSON-line parsing
JSONData := GetJSON(AJsonString);
// Checking that its array
if not (JSONData is TJSONArray) then
begin
WriteLn('Error: Waitng JSON-array.');
Exit;
end;
ZonesArray := TJSONArray(JSONData);
// Reviewing all areas
for ZoneItem in ZonesArray do
begin
ZoneObject := TJSONObject(ZoneItem.Value);
// Get data for
ZoneId := ZoneObject.Get('id', '');
ZoneName := ZoneObject.Get('name', '');
WriteLn('Zone ID: ', ZoneId);
WriteLn('Zone Name: ', ZoneName);
WriteLn('Regions:');
// Get an array of regions
if ZoneObject.Find('regions', JSONData) and (JSONData is TJSONArray) then
begin
for RegionItem in TJSONArray(JSONData) do
begin
RegionObject := TJSONObject(RegionItem.Value);
// Extracting region data
RegionId := RegionObject.Get('id', '');
RegionName := RegionObject.Get('name', '');
ZoneDesc := RegionObject.Get('zoneDescription', '');
ZoneRegionId := RegionObject.Get('zoneId', '');
WriteLn(' - Region ID: ', RegionId);
WriteLn(' Zone ID: ', ZoneRegionId);
WriteLn(' Name: ', RegionName);
WriteLn(' Description: ', ZoneDesc);
end;
end;
WriteLn('------------------');
end;
finally
if Assigned(JSONData) then
JSONData.Free; // Freeing up the memory
end;
end;