Recent

Author Topic: Passing value of JSON data to variable  (Read 4026 times)

CyberFilth

  • Jr. Member
  • **
  • Posts: 88
    • My github account
Passing value of JSON data to variable
« on: April 01, 2015, 05:41:07 pm »
Hi, I'm trying to get back into Pascal after a...20 year hiatus. I'm trying to familiarise myself with the syntax again by putting together a simple currency conversion program that grabs the current exchange rates from http://fixer.io/ in JSON format.
So far, I can download and display the exchange rate.
What I can't seem to do is pass the exchange rate into a variable.

This is what I have so far:
Code: [Select]
{$mode objfpc}{$H+}

uses fphttpclient, fpjson, jsonparser;

Var
  S : String;
  J: TJSONData;
  initialAmount, endAmount, rate: real;

begin
  With TFPHttpClient.Create(Nil) do
    try
      S:=Get('http://api.fixer.io/latest?base=GBP');
    finally
      Free;
    end;
    J:= GetJSON(S);
    writeln ('Current exchange rate of GBP to Polish złoty: ',J.FindPath('rates.PLN').AsFloat:2:2);
end.

This gets the exchange rate from GBP to Polish złoty and prints it to the screen. It works fine but what I want to do next is something along the lines of:
Code: [Select]
rate := J.FindPath('rates.PLN').AsFloat:2:2;
writeln;
write ('Enter initial amount in GBP £');
readln (initialAmount);
endAmount := initialAmount * rate;
writeln (endAmount);   

...and that's where my memory is kinda letting me down. I've read through the examples for parsing JSON but I can't seem to find a solution.

Any suggestions?
Running Windows 10 & Xubuntu 20.04 | Lazarus 2.0.12 | FPC 3.2.0

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Passing value of JSON data to variable
« Reply #1 on: April 01, 2015, 06:46:34 pm »
Code: [Select]
{$mode objfpc}{$H+}

uses fphttpclient, fpjson, jsonparser;

Var
  S : String;
  J: TJSONData;
  initialAmount, endAmount, rate: real; // or single or Double

begin
  With TFPHttpClient.Create(Nil) do
    try
      S:=Get('http://api.fixer.io/latest?base=GBP');
    finally
      Free;
    end;
    J:= GetJSON(S);
    rate := J.FindPath('rates.PLN').AsFloat;
    writeln ('Current exchange rate of GBP to Polish złoty: ',rate:2:2);
    writeln;
    write ('Enter initial amount in GBP £');
    readln (initialAmount);
    endAmount := initialAmount * rate;
    writeln (endAmount);   
end.
This has been typed directly on the browser form a quick look around I don't see any errors. If you have problems please post the error reported and I"ll try to fix it.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

CyberFilth

  • Jr. Member
  • **
  • Posts: 88
    • My github account
Re: Passing value of JSON data to variable
« Reply #2 on: April 01, 2015, 10:20:45 pm »
You've nailed it! Thanks for that.

So the only issue was truncating the result with
Code: [Select]
:2:2 ?
So is that only used for outputting a number?
Running Windows 10 & Xubuntu 20.04 | Lazarus 2.0.12 | FPC 3.2.0

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Passing value of JSON data to variable
« Reply #3 on: April 01, 2015, 11:37:39 pm »
You've nailed it! Thanks for that.

So the only issue was truncating the result with
Code: [Select]
:2:2 ?
So is that only used for outputting a number?

yes the specific syntax is part of the writeln and not the compiler.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

 

TinyPortal © 2005-2018