Passing value of JSON data to variable lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- CyberFilth · 2015-04-01 15:41 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? ---------------------------------------------------------------------- taazz · 2015-04-01 16:46 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. ---------------------------------------------------------------------- CyberFilth · 2015-04-01 20:20 You've nailed it! Thanks for that. So the only issue was truncating the result withCode: [Select]:2:2 ? So is that only used for outputting a number? ---------------------------------------------------------------------- taazz · 2015-04-01 21:37 Quote from: CyberFilth on April 01, 2015, 10:20:45 pmYou've nailed it! Thanks for that. So the only issue was truncating the result withCode: [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.