Following the Readers and Writers JSON Framework (which reduces memory consumption), I tried to use a RESTful service to get data via HTTP.
But I get an error :
E2010 Incompatible types: 'TJSONIterator' and 'string'
on the line:
LJRates := LJIter.AsDouble.ToString;
Async.Run<TStringList>(function: TStringList
var
LHTTP: THTTPClient;
LResp: IHTTPResponse;
LJIter: TJSONIterator;
LJRates: TJSONIterator;
LJTextR: TJsonTextReader;
LStrR: TStringReader;
// LResult: Boolean;
I: Integer;
begin
LHTTP := THTTPClient.Create;
try
LResp := LHTTP.Get('http://api.fixer.io/latest');
{$REGION 'check for errors'}
if LResp.StatusCode = 200 then
begin
// LResult := not LResp.ContentAsString(TEncoding.UTF8).IsEmpty
// or
// LStrR := TJSONObject.ParseJSONValue(LResp.ContentAsString(TEncoding.UTF8)) as TJSONObject;
// or
// LStrR := LResp.ContentAsString(TEncoding.UTF8);
// or
LStrR := TStringReader.Create(LResp.ContentAsString(TEncoding.UTF8));
end else
begin
raise Exception.CreateFmt('Cannot get rates. HTTP %d - %s', [LResp.StatusCode, LResp.StatusText]);
end;
{$ENDREGION}
LJTextR := TJsonTextReader.Create(LStrR);
LJIter := TJSONIterator.Create(LJTextR);
try
// gets the json object 'rates' and
LJIter.Recurse; // prepare to enter object
while LJIter.Next do;
begin
LJIter.Recurse; // enter object
LJIter.Next('rates');
LJRates := LJIter.AsDouble.ToString;
Result := TStringList.Create;
// loop through the property names
for I := 0 to LJRates.Depth - 1 do
begin
//add each names in the resulting TStringList
Result.Add(LJRates.Path[I]);
end;
Result.Sort;
end;
finally
LJIter.Free;
end;
finally
LHTTP.Free;
end;
end,
What is the correct way to iterates through the JSON, reading the content, and displaying the rate values?