Retrieving JSON data from URL in Delphi XE7 - json

I'm trying to get JSON data from an URL. The site I'm trying to connect is:
http://www.bitven.com/assets/js/rates.js
It contains the following JSON string:
{
"USD_TO_BSF_RATE": 112268.29,
"BTC_TO_USD_RATE": 13870.9,
"ETH_TO_USD_RATE": 752.222,
"BCH_TO_USD_RATE": 2960.81,
"LTC_TO_USD_RATE": 272.476,
"XRP_TO_USD_RATE": 1.01954,
"ETC_TO_USD_RATE": 31.1101,
"DASH_TO_USD_RATE": 1178.0,
"ZEC_TO_USD_RATE": 561.377,
"XMR_TO_USD_RATE": 354.709
}
I need to get the value of USD_TO_BSF_RATE, which is updated every 5 minutes in the site I mentioned. My code looks like:
uses
... System.JSON, IdHTTP;
function GetUrlContent(s: string): string;
var
IdHTTP1: TIdHTTP;
begin
IdHTTP1.Create;
GetUrlContent:=IdHTTP1.Get(s);
IdHTTP1.Destroy;
end;
procedure DolarUpdate;
var
json: string;
obj: TJSONObject;
url: string;
begin
try
json:=GetUrlContent('http://www.bitven.com/assets/js/rates.js');
try
obj := TJSONObject.ParseJSONValue(json) as TJSONObject;
TabbedForm.Edit2.Text := obj.Values['USD_TO_BSF_RATE'].Value;
finally
obj.Free;
end;
except
on E : Exception do
begin
ShowMessage('Error'+sLineBreak+E.ClassName+sLineBreak +E.Message);
end;
end;
end;
My app doesn't function correctly, nor return any messages. It only crashes.
What am I doing wrong?

Your GetUrlContent() function is not coded correctly. It needs to look like this instead:
function GetUrlContent(s: string): string;
var
IdHTTP1: TIdHTTP;
begin
IdHTTP1 := TIdHTTP.Create;
try
Result := IdHTTP1.Get(s);
finally
IdHTTP1.Free;
end;
end;
And your DolarUpdate() procedure should look more like this instead:
procedure DolarUpdate;
var
json: string;
obj: TJSONObject;
url: string;
begin
try
json := GetUrlContent('http://www.bitven.com/assets/js/rates.js');
obj := TJSONObject.ParseJSONValue(json) as TJSONObject;
if obj = nil then raise Exception.Create('Error parsing JSON');
try
TabbedForm.Edit2.Text := obj.Values['USD_TO_BSF_RATE'].Value;
finally
obj.Free;
end;
except
on E : Exception do
begin
ShowMessage('Error' + sLineBreak + E.ClassName + sLineBreak + E.Message);
end;
end;
end;

Related

JSON: Invalid Class Typecast in Delphi

This works, when it is an object:
{"data":{"url":"stackoverflow.com"}}
This does not work, when it is an array:
{"data":[{"url":"stackoverflow.com"}]}
Error: Invalid class typecast
procedure TForm1.Button1Click(Sender: TObject);
var
json : string;
obj, data : TJSONObject;
url : string;
begin
json := '{"data":{"url":"stackoverflow.com"}}';
obj := TJSonObject.ParseJSONValue(json) as TJSONObject;
try
data := obj.Values['data'] as TJSONObject;
url := data.Values['url'].value;
showMessage(url);
finally
obj.Free;
end;
end;
I know I have to use TJSONArray, but I don't know how to implement it.
This is not hard to understand.
In the first case, the value of data is an object, so obj.Values['data'] as TJSONObject is correct.
In the second case, the value of data is an array, so obj.Values['data'] as TJSONObject is wrong. it needs to be obj.Values['data'] as TJSONArray instead, and then you access the TJSONObject from the elements of the array, eg:
procedure TForm1.Button1Click(Sender: TObject);
var
json : string;
obj, data : TJSONObject;
arr: TJSONArray;
url : string;
begin
json := '{"data":[{"url":"stackoverflow.com"}]}';
obj := TJSonObject.ParseJSONValue(json) as TJSONObject;
try
arr := obj.Values['data'] as TJSONArray;
data := arr[0] as TJSONObject;
url := data.Values['url'].Value';
ShowMessage(url);
finally
obj.Free;
end;
end;

JSON object- how to iterate through all properties without knowing their names?

I have a fairly simple JSON:
[
{
"hero-img": "girl-ipad.png",
"main-story-headline": "How to Stay Mentally Healthy During a Pandemic",
"main-story-paragraph": "lorem ipsum",
"main-story-button-text": "Read More"
},
{
"hero-img": "painter-woman.png",
"main-story-headline": "How to Stay Mentally Healthy During a Pandemic",
"main-story-paragraph": "lorem ipsum",
"main-story-button-text": "Explore More"
},
{
"hero-img": "old-man.png",
"main-story-headline": "How to Stay Mentally Healthy During a Pandemic",
"main-story-paragraph": "lorem ipsum",
"main-story-button-text": "Get Yours Now"
} ]
By using Delphi 10.3 Rio, I want to iterate through all properties no matter how they are named. I've started this way:
program JuTemplates;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils, System.JSON, System.IOUtils, System.Types;
var
root, OutputDir, TemplatesDir, JsonDir, jsonText: string;
JsonFiles: TStringDynArray;
i, j: Integer;
JSONValue: TJSONObject;
JSONValues: TJSONArray;
begin
try
try
root := ExtractFilePath(ParamStr(0));
OutputDir := root + 'OutputDir\';
TemplatesDir := root + 'TemplatesDir\';
JsonDir := root + 'JsonDir\';
writeln('Processing: ' + JsonDir);
JsonFiles := TDirectory.GetFiles(JsonDir);
for i := 0 to High(JsonFiles) do
begin
jsonText := TFILE.ReadAllText(JsonFiles[i]);
JSONValues := TJSONObject.ParseJSONValue(jsonText) as TJSONArray;
for j := 0 to JSONValues.Count - 1 do
begin
JSONValue := JSONValues.Items[i] as TJSONObject;
// here I should iterate through that JSONValue object
end;
end;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
finally
writeln ('Press any key to continue');
readln;
end;
end.
To enumerate a JSONObject, you can use an enumerator like below.
Use for..in loop (implicit enumerator)
procedure TForm1.Button1Click(Sender: TObject);
var
JSONData : String;
JSONObject : TJSONObject;
JSONPair : TJSONPair;
begin
JSONData := '... some JSON data ...'; // Place you data here
JSONObject := TJSonObject.ParseJSONValue(JSONData) as TJSONObject;
try
for JSONPair in JSONObject do
ShowMessage(Format('1) Key=%s Value=%s',
[JSONPair.JsonString.ToString,
JSONPair.JsonValue.ToString]));
finally
JSONObject.Free;
end;
end;
Use a classic for loop:
procedure TForm1.Button1Click(Sender: TObject);
var
JSONData : String;
JSONObject : TJSONObject;
JSONPair : TJSONPair;
I : Integer;
begin
JSONData := '... some JSON data ...'; // Place you data here
JSONObject := TJSonObject.ParseJSONValue(JSONData) as TJSONObject;
try
for I := 0 to JSONObject.Count - 1 do begin
ShowMessage(Format('Key=%s Value=%s',
[JSONObject.Pairs[I].JsonString.ToString,
JSONObject.Pairs[I].JsonValue.ToString]));
end;
finally
JSONObject.Free;
end;
end;
Use an explicit enumerator and a while loop:
procedure TForm1.Button1Click(Sender: TObject);
var
JSONData : String;
JSONObject : TJSONObject;
JSONEnumerator : TJSONObject.TEnumerator;
JSONPair : TJSONPair;
begin
JSONData := '... some JSON data ...'; // Place you data here
JSONObject := TJSonObject.ParseJSONValue(JSONData) as TJSONObject;
try
JSONEnumerator := JSONObject.GetEnumerator;
try
while JSONEnumerator.MoveNext do begin
JSONPair := JSONEnumerator.Current;
ShowMessage(Format('Key=%s Value=%s',
[JSONPair.JsonString.ToString,
JSONPair.JsonValue.ToString]));
end;
finally
JSONEnumerator.Free;
end;
finally
JSONObject.Free;
end;
end;
Note that you may need to check if JSONPair.JsonValue has childs and enumerate those childs with another enumerator, recursively.

D10 to DXE5: what's the equivalent of TJSONBool.Create(False) and JSONObj.ToJSON?

The folowing code compiles fine on D10 Seattle, but my pc where have D10 is installed is broken. Then i'm needing make a small update in my project using DXE5 but not compiles because the commands TJSONBool.Create(False) and JSONObj.ToJSON are present.
what's are equivalent of TJSONBool.Create(False) and JSONObj.ToJSON respectivally on DXE5?
uses
Data.DBXJSON, SHFolder;
function GetSpecialFolderPath(folder : integer) : string;
const
SHGFP_TYPE_CURRENT = 0;
var
path: array [0..MAX_PATH] of char;
begin
if SUCCEEDED(SHGetFolderPath(0,folder,0,SHGFP_TYPE_CURRENT,#path[0])) then
Result := path
else
Result := '';
end;
procedure ChangeChromeSetting(const ATarget, Avalue: string);
var
specialfolder: integer;
pathchrome: String;
JSONObj, ObjIpp: TJSONObject;
JSONPair: TJSONPair;
OldValue: string;
begin
specialFolder := CSIDL_LOCAL_APPDATA;
pathchrome := GetSpecialFolderPath(specialFolder);
pathchrome := pathchrome + '\Google\Chrome\User Data\Local State';
if fileexists(pathchrome) then
begin
JSONObj := TJSONObject.ParseJSONValue(TFile.ReadAllText(pathchrome)) as TJSONObject;
if not Assigned(JSONObj) then Exit; {raise Exception.Create('Cannot read file: ' + pathchrome);}
try
OldValue := JSONObj.GetValue<string>(ATarget);
if OldValue = '' then
Exit;
if not SameText(OldValue, Avalue) then
begin
JSONPair := JSONObj.Get(ATarget);
JSONPair.JsonValue.Free;
JSONPair.JsonValue := TJSONString.Create(Avalue);
ObjIpp := TJSONObject.Create;
ObjIpp.AddPair('enabled', TJSONBool.Create(False));
JSONObj.AddPair('hardware_acceleration_mode', ObjIpp);
TFile.WriteAllText(pathchrome, JSONObj.ToJSON);
end;
finally
JSONObj.Free;
end;
end;
end;
////////////////////// USAGE /////////////////////////
ChangeChromeSetting('hardware_acceleration_mode_previous', 'false');
TJSONBool and TJSONAncestor.AsJSON didn't exist yet in XE5. ToJSON was added in XE7, and TJSONBool was added in 10.0 Seattle.
In older versions, use TJSONTrue/TJSONFalse and TJSONObject.ToString instead:
ObjIpp.AddPair('enabled', TJSONFalse.Create);
...
TFile.WriteAllText(pathchrome, JSONObj.ToString);

Get Objects in a json format?

I wrote the below code in Delphi 2010 to download a JSON string:
procedure TForm1.Button1Click(Sender: TObject);
var
strResult: string;
listParams: TStringList;
JO :TJSONObject;
JV : TJSONValue;
begin
listParams := TStringList.Create;
listParams.Add('action=GET');
listParams.Add('userid=(11,12,13)');
try
strResult := idhttp1.Post('http://xxxnet/api/users.php', listParams);
Memo1.Lines.Text:=strResult;
JO := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(strResult), 0) as TJSONObject;
JV := JO.Get(0).JsonValue;
Memo2.Lines.Add(JV.Value);
finally
listParams.Free;
end;
end;
When the JSON contains a single object:
{"usertitle":"Mark","userid":"13","username":"950","useremail":"","success":"1","error":""}
The code works good.
But when the JSON contains multi objects:
[{"usertitle":"Yani","userid":"11","username":"887","useremail":"nili_orusoft#yahoo.com","success":"1","error":""},{"usertitle":"Frank","userid":"12","username":"851","useremail":"","success":"1","error":""},{"usertitle":"Mark","userid":"13","username":"950","useremail":"","success":"1","error":""}]
The code crashes with an "access violation at address 00522275" error.
There are two problems with your code:
You are leaking the object that ParseJSONValue() returns. You need to Free() it when you are done using it.
Your second JSON example is an array of objects. ParseJSONValue() will return a TJSONArray instead of a TJSONObject, so your as TJSONObject typecast will fail and raise an exception (but it should not be raising an access violation).
Try this code instead:
procedure TForm1.Button1Click(Sender: TObject);
var
strResult: string;
listParams: TStringList;
JA: TJSONArray;
JO: TJSONObject;
JV, JV2: TJSONValue;
begin
listParams := TStringList.Create;
try
listParams.Add('action=GET');
listParams.Add('userid=(11,12,13)');
strResult := idhttp1.Post('http://xxxnet/api/users.php', listParams);
finally
listParams.Free;
end;
Memo1.Lines.Text := strResult;
JV := TJSONObject.ParseJSONValue(TEncoding.UTF8.GetBytes(strResult), 0);
try
if JV is TJSONObject then
begin
JO := TJSONObject(JV);
JV2 := JO.Get(0).JsonValue;
Memo2.Lines.Add(JV2.Value);
end
else if JV is TJSONArray then
begin
JA := TJSONArray(JV);
JO := JA.Get(0) as TJSONObject;
JV2 := JO.Get(0).JsonValue;
Memo2.Lines.Add(JV2.Value);
end;
finally
JV.Free;
end;
end;

How to parse a JSON string in Inno Setup?

I have the following JSON:
{
"Info": {
"User": 2,
"String": "foo"
}
}
Unfortunately TLama's Inno JSON Config library doesn't work with JSON strings but only with json files.
I tried to use JSON string instead of path to json file, but it didn't work.
if JSONQueryInteger('{"Info":{"User":2,"String":"foo"}}', 'Info', 'User', 0, IntValue) then
MsgBox('User=' + IntToStr(IntValue), mbInformation, MB_OK);
I know I could save my JSON to a file and then parse it but it seems kind of messy.
How to parse a JSON string in Inno Setup?
You can use JsonParser library instead. It can parse JSON strings.
It's not as easy to use as JSONConfig.dll – but that's the reason why it is more flexible. Also it's a native Pascal Script code. So, it not only saves you from a temporary .json file, but also from a temporary .dll.
The code can be like:
[Code]
#include "JsonParser.pas"
function GetJsonRoot(Output: TJsonParserOutput): TJsonObject;
begin
Result := Output.Objects[0];
end;
function FindJsonValue(
Output: TJsonParserOutput; Parent: TJsonObject; Key: TJsonString;
var Value: TJsonValue): Boolean;
var
I: Integer;
begin
for I := 0 to Length(Parent) - 1 do
begin
if Parent[I].Key = Key then
begin
Value := Parent[I].Value;
Result := True;
Exit;
end;
end;
Result := False;
end;
function FindJsonObject(
Output: TJsonParserOutput; Parent: TJsonObject; Key: TJsonString;
var Object: TJsonObject): Boolean;
var
JsonValue: TJsonValue;
begin
Result :=
FindJsonValue(Output, Parent, Key, JsonValue) and
(JsonValue.Kind = JVKObject);
if Result then
begin
Object := Output.Objects[JsonValue.Index];
end;
end;
function FindJsonNumber(
Output: TJsonParserOutput; Parent: TJsonObject; Key: TJsonString;
var Number: TJsonNumber): Boolean;
var
JsonValue: TJsonValue;
begin
Result :=
FindJsonValue(Output, Parent, Key, JsonValue) and
(JsonValue.Kind = JVKNumber);
if Result then
begin
Number := Output.Numbers[JsonValue.Index];
end;
end;
function FindJsonString(
Output: TJsonParserOutput; Parent: TJsonObject; Key: TJsonString;
var Str: TJsonString): Boolean;
var
JsonValue: TJsonValue;
begin
Result :=
FindJsonValue(Output, Parent, Key, JsonValue) and
(JsonValue.Kind = JVKString);
if Result then
begin
Str := Output.Strings[JsonValue.Index];
end;
end;
function ParseJsonAndLogErrors(
var JsonParser: TJsonParser; const Source: WideString): Boolean;
var
I: Integer;
begin
ParseJson(JsonParser, Source);
Result := (Length(JsonParser.Output.Errors) = 0);
if not Result then
begin
Log('Error parsing JSON');
for I := 0 to Length(JsonParser.Output.Errors) - 1 do
begin
Log(JsonParser.Output.Errors[I]);
end;
end;
end;
procedure ParseJsonString;
var
Json: string;
JsonParser: TJsonParser;
I: Integer;
JsonRoot, InfoObject: TJsonObject;
UserNumber: TJsonNumber; { = Double }
UserString: TJsonString; { = WideString = string }
begin
Json := '{"Info":{"User":2,"String":"abc"}}';
if ParseJsonAndLogErrors(JsonParser, Json) then
begin
JsonRoot := GetJsonRoot(JsonParser.Output);
if FindJsonObject(JsonParser.Output, JsonRoot, 'Info', InfoObject) and
FindJsonNumber(JsonParser.Output, InfoObject, 'User', UserNumber) and
FindJsonString(JsonParser.Output, InfoObject, 'String', UserString) then
begin
Log(Format('Info:User:%d', [Round(UserNumber)]));
Log(Format('Info:String:%s', [UserString]));
end;
end;
ClearJsonParser(JsonParser);
end;
Another option is to fork the Inno JSON Config library and add support for parsing strings.