I have such a file:
[
{
"FirstName": "Oleg",
"Surname": "Buckley"
},
{
"FirstName": "Amery",
"Surname": "Mcmillan"
},
{
"FirstName": "Denton",
"Surname": "Burnett"
....
I want to be able to read them into my program. So far, I have completed this very small function: p>
function GetGeneratedNames: TArray;
var fileName: TFileName;
JSONValue, jv: TJSONValue;
JSONArray: TJSONArray;
jo: TJSONObject;
pair: TJSONPair;
begin
result := nil;
filename := ExePath +'Names.json';
JSONValue := TJSONObject .ParseJSONValue(TEncoding.ASCII.GetBytes(TFile.ReadAllText(filename)), 0);
if JSONValue is TJSONArray then begin
for jv in (JSONValue as TJSONArray) do begin
if jv is TJSONObject then begin
jo := jv as TJSONObject;
for pair in jo do begin
Append(result, jo.Value);
end;
end;
end;
end;
end{ GetGeneratedNames};
Trouble The thing is, it returns an empty string array. Can anyone point me in the right direction?
TIA
tag
// XE5- version
uses System.SysUtils, Data.DBXJSON, System.IOUtils;
function GetGeneratedNames: TArray;
var
fileName: TFileName;
JSONValue, jv: TJSONValue;
begin
fileName := TPath.Combine(ExePath,'Names.json');
JSONValue := TJSONObject.ParseJSONValue(TFile.ReadAllText(fileName));
try
if JSONValue is TJSONArray then
begin
for jv in TJSONArray(JSONValue) do
begin
Append(Result, (jv as TJSONObject). Get('FirstName').JSONValue.Value);
Append(Result, (jv as TJSONObject).Get('Surname').JSONValue.Value);
end;
end;
finally
JSONValue.Free;
end;
end {GetGeneratedNames };
// XE6+ version
uses System.SysUtils, System .JSON, System.IOUtils;
function GetGeneratedNames: TArray;
var
fil eName: TFileName;
JSONValue, jv: TJSONValue;
begin
fileName := TPath.Combine(ExePath,'Names.json');
JSONValue := TJSONObject.ParseJSONValue( TFile.ReadAllText(fileName));
try
if JSONValue is TJSONArray then
begin
for jv in TJSONArray(JSONValue) do
begin
Append( Result, jv.GetValue('FirstName'));
Append(Result, jv.GetValue('Surname'));
end;
end;
finally
JSONValue.Free;
end;
end {GetGeneratedNames };
I have been trying to solve this problem, do Some simple things seem to take too long.
I have a file like this:
[
{
"FirstName": "Oleg",
"Surname": "Buckley"
},
{
"FirstName": "Amery",
"Surname" : "Mcmillan"
},
{
"FirstName": "Denton",
"Surname": "Burnett"
....
I want to be able to read them into my program. So far, I have completed this very small function:
function GetGeneratedNames: TArray;
var fileN ame: TFileName;
JSONValue, jv: TJSONValue;
JSONArray: TJSONArray;
jo: TJSONObject;
pair: TJSONPair;
begin
result := nil;
filename := ExePath +'Names.json';
JSONValue := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(TFile.ReadAllText(filename)), 0);
if JSONValue is TJSONArray then begin
for jv in (JSONValue as TJSONArray) do begin
if jv is TJSONObject then begin
jo := jv as TJSONObject;
for pair in jo do begin
Append(result, jo.Value);
end;
end;
end;
end;
end{ GetGeneratedNames};
The trouble is that it returns an empty string array. Can anyone point me in the right direction?
TIA
Tag
// XE5- version
uses System.SysUtils, Data.DBXJSON , System.IOUtils;
function GetGeneratedNames: TArray;
var
fileName: TFileName;
JSONValue, jv: TJSONValue;
begin< br /> fileName := TPath.Combine(ExePath,'Names.json');
JSONValue := TJSONObject.ParseJSONValue(TFile.ReadAllText(fileName));
try
if JSONValue is TJSONArray then
begin
for jv in TJSONArray(JSONValue) do
begin
Append(Result, (jv as TJSONObject).Get('FirstName').JSONValue.Value);
Append(Result, (jv as TJSONObject).Get('Surname').JSONValue.Value);
end;
end;
finally
JSONValue.Free ;
end;
end {GetGeneratedNames };
// XE6+ version
uses System.SysUtils, System.JSON, System.IOUtils;
< br />function GetGeneratedNames: TArray;
var
fileName: TFileName;
JSONValue, jv: TJSONValue;
begin
fileName := TPath.Combine(ExePath,'Names.json');
JSONValue := TJSONObject.ParseJSONValue(TFile.ReadAllText(fileName));
try< br /> if JSONValue is TJSONArray then
begin
for jv in TJSONArray(JSONValue) do
begin
Append(Result, jv.GetValue('FirstName')) ;
Append(Result, jv.GetValue('Surname'));
end;
end;
finally
JSONValue.Free;
end;
end {GetGeneratedNames };