Skip to content

Instantly share code, notes, and snippets.

@turric4n
Created July 1, 2018 21:39
Show Gist options
  • Save turric4n/e915428cf5ad66bb93f4d769b4c5a4ca to your computer and use it in GitHub Desktop.
Save turric4n/e915428cf5ad66bb93f4d769b4c5a4ca to your computer and use it in GitHub Desktop.
Parsing JSON for Ulugbek Nurjanov
program JSONParsingExample;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
System.Net.HttpClient,
System.Generics.Collections,
System.JSON;
type
TValute = class(TObject)
private
FName: string;
FNominal: Integer;
FNumCode: string;
FID: string;
FCharCode: string;
FPrevious: Single;
FValue: Single;
procedure SetCharCode(const Value: string);
procedure SetID(const Value: string);
procedure SetName(const Value: string);
procedure SetNominal(const Value: Integer);
procedure SetNumCode(const Value: string);
procedure SetPrevious(const Value: Single);
procedure SetValue(const Value: Single);
public
property ID : string read FID write SetID;
property NumCode : string read FNumCode write SetNumCode;
property CharCode : string read FCharCode write SetCharCode;
property Nominal : Integer read FNominal write SetNominal;
property Name : string read FName write SetName;
property Value : Single read FValue write SetValue;
property Previous : Single read FPrevious write SetPrevious;
end;
TSomeClass = class(TObject)
private
FDate: string;
FPreviousDate: string;
FPreviousURL: string;
FTimeStamp: string;
FValutes: TObjectlist<TValute>;
procedure SetDate(const Value: string);
procedure SetPreviousDate(const Value: string);
procedure SetPreviousURL(const Value: string);
procedure SetTimeStamp(const Value: string);
procedure SetValutes(const Value: TObjectlist<TValute>);
public
constructor Create;
destructor Destroy; override;
property Date : string read FDate write SetDate;
property PreviousDate : string read FPreviousDate write SetPreviousDate;
property PreviousURL : string read FPreviousURL write SetPreviousURL;
property TimeStamp : string read FTimeStamp write SetTimeStamp;
//We threat Valutes as ObjectList (Cleaner)
property Valutes : TObjectlist<TValute> read FValutes write SetValutes;
end;
var
httpresponse : IHTTPResponse;
jsoncontent : string;
{ TSomeClass }
constructor TSomeClass.Create;
begin
FValutes := TObjectList<TValute>.Create(True);
end;
destructor TSomeClass.Destroy;
begin
FValutes.Free;
inherited;
end;
procedure TSomeClass.SetDate(const Value: string);
begin
FDate := Value;
end;
procedure TSomeClass.SetPreviousDate(const Value: string);
begin
FPreviousDate := Value;
end;
procedure TSomeClass.SetPreviousURL(const Value: string);
begin
FPreviousURL := Value;
end;
procedure TSomeClass.SetTimeStamp(const Value: string);
begin
FTimeStamp := Value;
end;
procedure TSomeClass.SetValutes(const Value: TObjectlist<TValute>);
begin
FValutes := Value;
end;
{ TValute }
procedure TValute.SetCharCode(const Value: string);
begin
FCharCode := Value;
end;
procedure TValute.SetID(const Value: string);
begin
FID := Value;
end;
procedure TValute.SetName(const Value: string);
begin
FName := Value;
end;
procedure TValute.SetNominal(const Value: Integer);
begin
FNominal := Value;
end;
procedure TValute.SetNumCode(const Value: string);
begin
FNumCode := Value;
end;
procedure TValute.SetPrevious(const Value: Single);
begin
FPrevious := Value;
end;
procedure TValute.SetValue(const Value: Single);
begin
FValue := Value;
end;
function ParseValute(JSONObject : TJSONObject) : TArray<TValute>;
begin
//TODO.
end;
function ParseJSONResponse(const aJSON : string) : TSomeClass;
var
vJSONScenario : TJSONValue;
vJSONPair : TJSONPair;
a : TDateTime;
x : Integer;
begin
vJSONScenario := TJSONValue.Create;
vJSONScenario := TJSONObject.ParseJSONValue(aJSON, False);
if vJSONScenario = nil then raise Exception.Create('Invalid JSON string!')
else if vJSONScenario is TJSONArray then raise Exception.Create('Passed JSON is an Array!');
Result := TSomeClass.Create;
for x := 0 to TJSONObject(vJSONScenario).Count - 1 do
begin
vJSONPair := TJSONObject(vJSONScenario).Pairs[x];
if vJSONPair.JsonString.Value = 'Date' then Result.Date := TJSONObject(vJSONScenario).GetValue('Date').ToString.Replace('"','');
if vJSONPair.JsonString.Value = 'PreviousDate' then Result.PreviousDate := TJSONObject(vJSONScenario).GetValue('PreviousDate').ToString.Replace('"','');
if vJSONPair.JsonString.Value = 'PreviousURL' then Result.PreviousURL := TJSONObject(vJSONScenario).GetValue('PreviousURL').ToString.Replace('"','');
if vJSONPair.JsonString.Value = 'Timestamp' then Result.TimeStamp := TJSONObject(vJSONScenario).GetValue('Timestamp').ToString.Replace('"','');
if vJSONPair.JsonString.Value = 'Valute' then Result.Valutes.AddRange(ParseValute(vJSONScenario.GetValue('Valute') as TJSONObject));
end;
end;
begin
try
{ TODO -oUser -cConsole Main : Insert code here }
with THTTPClient.Create do
begin
try
httpresponse := Get('https://www.cbr-xml-daily.ru/daily_json.js');
if httpresponse.ContentLength = 0 then raise Exception.Create('Empty response')
else if httpresponse.HeaderValue['content-type'] <> 'application/javascript' then raise Exception.Create('Invalid content type... not a real JSON response');
jsoncontent := httpresponse.ContentAsString();
finally
Free;
end;
end;
// We need to parse response
ParseJSONResponse(jsoncontent);
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment