Delphi XE5 JSON nested object - json

I am trying to generate JSON, data source is a DB FireBird.
I have a schema to generate path like this:
schema:= TDictionary<string, string>.Create;
schema.Add('DBTableName1', 'nest1.valueKeyName1');
schema.Add('DBTableName2', 'nest1.valueKeyName2');
schema.Add('DBTableName3', 'nest2.valueKeyName1');
schema.Add('DBTableName4', 'nest3.valueKeyName1');
schema.Add('DBTableName5', 'nest3.valueKeyName2');
schema.Add('DBTableName6', 'nest4.valueKeyName1');
How to create function for generate path to make nested objects?
{
"nest1": {
valueKeyName1: DBTableValue1,
valueKeyName2: DBTableValue2,
},
"nest2": {
valueKeyName1: DBTableValue3
},
"nest5":{
"nest6": {
"key1": "value1",
"key2": "value2",
},
"nest7": {}
}
In JavaScript I can do something like:
if (object.hasOwnProperty['key'] == false) object['key'] = {};
object = object['key'];
But in Delphi I have problem, and do not know, how to go deeper:
function TKlient.wprowadzWartoscDoJSON(wartosc: string; JSON: TJSONObject; sciezka: TStringList): TJSONObject;
var
i: integer;
obiekt: TJSONObject;
para: TJSONPair;
zagniezdzen: integer;
begin
zagniezdzen := sciezka.Count - 2;
obiekt := JSON;
para:= obiekt.Get(sciezka[i]);
for i := 1 to zagniezdzen do
begin
if obiekt.Get(sciezka[i]) = nil then obiekt.AddPair(sciezka[i], TJSONObject.Create)
else obiekt := obiekt.Get(sciezka[i]);
end;
obiekt.AddPair(sciezka[sciezka.Count - 1], wartosc);
result := obiekt;
end;

Here I've used a TDictionary<string, TJSONObject> to keep track of the object paths.
var
schema: TDictionary<string, string>;
pathSchema: TDictionary<string{path to every object}, TJSONObject>;
pair: TPair<string, string>;
values: TStringDynArray;
jsonObj,
jsonChildObj,
jsonParentObj: TJSONObject;
path: string;
i: Integer;
begin
schema := TDictionary<string, string>.Create;
try
schema.Add('DBTableName1', 'nest1.valueKeyName1');
schema.Add('DBTableName2', 'nest1.valueKeyName2');
schema.Add('DBTableName3', 'nest2.valueKeyName1');
schema.Add('DBTableName4', 'nest3.valueKeyName1');
schema.Add('DBTableName5', 'nest3.valueKeyName2');
schema.Add('DBTableName6', 'nest4.valueKeyName1');
schema.Add('value1', 'nest5.nest6.key1');
schema.Add('value2', 'nest5.nest6.key2');
pathSchema := TDictionary<string, TJSONObject>.Create;
try
jsonObj := TJSONObject.Create;
try
for pair in schema do begin
values := SplitString(pair.Value, '.');
path := '';
jsonParentObj := jsonObj;
for i := Low(values) to High(values)-1 do begin
if i > 0 then
path := path + '.';
path := path + values[i];
if pathSchema.ContainsKey(path) then
jsonChildObj := pathSchema[path]
else begin
jsonChildObj := TJSONObject.Create;
jsonParentObj.AddPair(TJSONPair.Create(values[i], jsonChildObj));
pathSchema.Add(path, jsonChildObj);
end;
jsonParentObj := jsonChildObj;
end;
jsonChildObj.AddPair(TJSONPair.Create(values[High(values)], pair.Key));
end;
WriteLn(jsonObj.ToString);
finally
jsonObj.Free;
end;
finally
pathSchema.Free;
end;
finally
schema.Free;
end;
ReadLn;
end.
The above prints the following:
{
"nest4":{
"valueKeyName1":"DBTableName6"
},
"nest2":{
"valueKeyName1":"DBTableName3"
},
"nest5":{
"nest6":{
"key1":"value1",
"key2":"value2"
}
},
"nest1":{
"valueKeyName1":"DBTableName1",
"valueKeyName2":"DBTableName2"
},
"nest3":{
"valueKeyName1":"DBTableName4",
"valueKeyName2":"DBTableName5"
}
}

Related

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.

How to parse this json data in Delphi 10 Seattle?

I am having trouble parsing this JSON data in Delphi 10 Seattle. I want to get the values from the JSON and show them in TLabel components one by one. I am new to JSON and REST, it would be nice if you provide a working example.
{
"countrydata":[
{
"info":{
"ourid":119,
"title":"Pakistan",
"code":"PK",
"source":"https://thevirustracker.com/pakistan-coronavirus-information-pk"
},
"total_cases":5038,
"total_recovered":1026,
"total_unresolved":0,
"total_deaths":86,
"total_new_cases_today":27,
"total_new_deaths_today":0,
"total_active_cases":3926,
"total_serious_cases":37,
"total_danger_rank":33
}
],
"stat":"ok"
}
EDIT
So far, I have this code but it gives access violation error and no output is given. What am I doing wrong.
procedure TForm1.Button2Click(Sender: TObject);
var
jsonRoot: TJSONObject;
tokenRequest: TRESTRequest;
tokenResponse: TRESTResponse;
tokenClient: TRESTClient;
begin
tokenClient := TRESTClient.Create(nil);
tokenRequest := TRESTRequest.Create(nil);
tokenResponse := TRESTResponse.Create(nil);
try
tokenRequest.Client := tokenClient;
tokenRequest.Response := tokenResponse;
tokenClient.BaseURL := 'https://api.thevirustracker.com/free-api?countryTotal=PK';
tokenRequest.Execute;
jsonRoot:= TJSONObject.ParseJSONValue(tokenResponse.JSONText) as TJSONObject;
Memo1.Lines.Add('TotalCases => ' + jsonRoot.GetValue('total_cases').Value);
Memo1.Lines.Add('TotalRecovered=> ' + jsonRoot.GetValue('total_recovered').Value);
Memo1.Lines.Add('TotalDeaths=> ' + jsonRoot.GetValue('total_deaths').Value);
Memo1.Lines.Add('TotoalNewCases=> ' + jsonRoot.GetValue('total_new_cases_today').Value);
finally
tokenResponse.Free;
tokenRequest.Free;
tokenClient.Free;
end;
end;
You are getting an Access Violation because you are using TJSONObject incorrectly.
All of the JSON values you are trying to read are not immediate children of the top level object of the JSON hierarchy, where you are expecting, so GetValue() returns a nil pointer, and then your code crashes when it tries to read the Value property using that nil pointer.
The values you want are several levels deeper in the JSON hierarchy. The top level object contains a child named countrydata, which is an array of objects. The values you want are children of the 1st object in that array.
Try this instead:
procedure TForm1.Button2Click(Sender: TObject);
var
jsonRoot: TJSONValue;
jsonObj: TJSONObject;
jsonArr: TJSONArray;
tokenRequest: TRESTRequest;
tokenResponse: TRESTResponse;
tokenClient: TRESTClient;
begin
tokenClient := TRESTClient.Create(nil);
try
tokenClient.BaseURL := 'https://api.thevirustracker.com/free-api?countryTotal=PK';
tokenRequest := TRESTRequest.Create(tokenClient);
tokenRequest.Client := tokenClient;
tokenResponse := TRESTResponse.Create(tokenClient);
tokenRequest.Response := tokenResponse;
tokenRequest.Execute;
jsonRoot := TJSONObject.ParseJSONValue(tokenResponse.JSONText);
try
jsonObj := jsonRoot as TJSONObject;
jsonArr := jsonObj.GetValue('countrydata') as TJSONArray;
jsonObj := jsonArr.Items[0] as TJSONObject;
Memo1.Lines.Add('TotalCases => ' + jsonObj.GetValue('total_cases').Value);
Memo1.Lines.Add('TotalRecovered=> ' + jsonObj.GetValue('total_recovered').Value);
Memo1.Lines.Add('TotalDeaths=> ' + jsonObj.GetValue('total_deaths').Value);
Memo1.Lines.Add('TotoalNewCases=> ' + jsonObj.GetValue('total_new_cases_today').Value);
finally
jsonRoot.Free;
end;
finally
tokenClient.Free;
end;
end;
Alternatively, you can use the TRESTResponse.RootElement and TRESTResponse.JSONValue properties instead of calling TJSONObject.ParseJSONValue() manually:
procedure TForm1.Button2Click(Sender: TObject);
var
jsonObj: TJSONObject;
jsonArr: TJSONArray;
tokenRequest: TRESTRequest;
tokenResponse: TRESTResponse;
tokenClient: TRESTClient;
begin
tokenClient := TRESTClient.Create(nil);
try
tokenClient.BaseURL := 'https://api.thevirustracker.com/free-api?countryTotal=PK';
tokenRequest := TRESTRequest.Create(tokenClient);
tokenRequest.Client := tokenClient;
tokenResponse := TRESTResponse.Create(tokenClient);
tokenRequest.Response := tokenResponse;
tokenResponse.RootElement := 'countrydata';
tokenRequest.Execute;
jsonArr := tokenResponse.JSONValue as TJSONArray;
jsonObj := jsonArr.Items[0] as TJSONObject;
Memo1.Lines.Add('TotalCases => ' + jsonObj.GetValue('total_cases').Value);
Memo1.Lines.Add('TotalRecovered=> ' + jsonObj.GetValue('total_recovered').Value);
Memo1.Lines.Add('TotalDeaths=> ' + jsonObj.GetValue('total_deaths').Value);
Memo1.Lines.Add('TotoalNewCases=> ' + jsonObj.GetValue('total_new_cases_today').Value);
finally
tokenClient.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.

Build JSON with Delphi

I'm trying to implement a function which returns a json object containing elements of class. Here is my function:
procedure ListToJson(AInputList: TList<TRating>;
AResponse: TJSONObject);
var
i: Integer;
jsonPair: TJSONPair;
jsonObject: TJSONObject;
jsonArray: TJSONArray;
begin
jsonArray := TJSONArray.Create();
jsonObject := TJSONObject.Create();
jsonPair := TJSONPair.Create('ratings', jsonArray);
for i := 0 to AInputList.Count - 1 do
begin
jsonObject.AddPair(TJSONPair.Create('idrating', IntToStr(AInputList[i].IdRating)));
jsonObject.AddPair(TJSONPair.Create('idmark', IntToStr(AInputList[i].IdMark)));
jsonObject.AddPair(TJSONPair.Create('value', IntToStr(AInputList[i].Value)));
jsonObject.AddPair(TJSONPair.Create('description', AInputList[i].Description));
jsonObject.AddPair(TJSONPair.Create('timeposted', FormatDateTime('yyyy-mm-dd hh:mm:ss', AInputList[i].TimePosted)));
jsonArray.AddElement(jsonObject);
end;
AResponse.AddPair(jsonPair);
end;
And when I test it with a list which contains two elements the returned string is:
{
"ratings":[{
"idrating":"1",
"idmark":"0",
"value":"0",
"description":"",
"timeposted":"2015-07-29 11:25:03",
"idrating":"2",
"idmark":"0",
"value":"0",
"description":"",
"timeposted":"2015-07-29 11:25:24"
},{
"idrating":"1",
"idmark":"0",
"value":"0",
"description":"",
"timeposted":"2015-07-29 11:25:03",
"idrating":"2",
"idmark":"0",
"value":"0",
"description":"",
"timeposted":"2015-07-29 11:25:24"
}]
}
I tried to remove all pairs after each loop iteration:
procedure ListToJson(AInputList: TList<TRating>;
AResponse: TJSONObject);
var
i: Integer;
jsonPair: TJSONPair;
jsonObject: TJSONObject;
jsonArray: TJSONArray;
begin
jsonArray := TJSONArray.Create();
jsonObject := TJSONObject.Create();
jsonPair := TJSONPair.Create('ratings', jsonArray);
for i := 0 to AInputList.Count - 1 do
begin
jsonObject.AddPair(TJSONPair.Create('idrating', IntToStr(AInputList[i].IdRating)));
jsonObject.AddPair(TJSONPair.Create('idmark', IntToStr(AInputList[i].IdMark)));
jsonObject.AddPair(TJSONPair.Create('value', IntToStr(AInputList[i].Value)));
jsonObject.AddPair(TJSONPair.Create('description', AInputList[i].Description));
jsonObject.AddPair(TJSONPair.Create('timeposted', FormatDateTime('yyyy-mm-dd hh:mm:ss', AInputList[i].TimePosted)));
jsonArray.AddElement(jsonObject);
jsonObject.RemovePair('idrating');
jsonObject.RemovePair('idmark');
jsonObject.RemovePair('value');
jsonObject.RemovePair('description');
jsonObject.RemovePair('timeposted');
end;
AResponse.AddPair(jsonPair);
end;
And the ouput is an array with n(count of list elements) empty objects: {"ratings":[{},{}]}
And the json I'm trying to build should be like:
{
"ratings":[{
"idrating":"1",
"idmark":"0",
"value":"0",
"description":"",
"timeposted":"2015-07-29 11:25:03"
},{
"idrating":"2",
"idmark":"0",
"value":"0",
"description":"",
"timeposted":"2015-07-29 11:25:24"
}]
}
You have one jsonObject, so whatever state it ends up in is repeated each time you add it to the array.
Construct jsonObject within the loop, and then you will have different objects in your array.
for i := 0 to AInputList.Count - 1 do
begin
jsonObject := TJSONObject.Create();
jsonObject.AddPair(TJSONPair.Create('idrating', IntToStr(AInputList[i].IdRating)));
jsonObject.AddPair(TJSONPair.Create('idmark', IntToStr(AInputList[i].IdMark)));
jsonObject.AddPair(TJSONPair.Create('value', IntToStr(AInputList[i].Value)));
jsonObject.AddPair(TJSONPair.Create('description', AInputList[i].Description));
jsonObject.AddPair(TJSONPair.Create('timeposted', FormatDateTime('yyyy-mm-dd hh:mm:ss', AInputList[i].TimePosted)));
jsonArray.AddElement(jsonObject);
end;
Alternatly, you can define a function to create a TJSONObject from a TRating, and use that in your loop
function TRatingToJSON( Rating: TRating ): TJSONObject;
begin
Result := TJSONObject.Create();
Result.AddPair(TJSONPair.Create('idrating', IntToStr(Rating.IdRating)));
Result.AddPair(TJSONPair.Create('idmark', IntToStr(Rating.IdMark)));
Result.AddPair(TJSONPair.Create('value', IntToStr(Rating.Value)));
Result.AddPair(TJSONPair.Create('description', Rating.Description));
Result.AddPair(TJSONPair.Create('timeposted', FormatDateTime('yyyy-mm-dd hh:mm:ss', Rating.TimePosted)));
end;
procedure ListToJson(AInputList: TList<TRating>;
AResponse: TJSONObject);
var
i: Integer;
jsonArray: TJSONArray;
begin
jsonArray := TJSONArray.Create();
for i := 0 to AInputList.Count - 1 do
begin
jsonArray.AddElement(TRatingToJSON(AInputList[i]));
end;
AResponse.AddPair(TJSONPair.Create('ratings', jsonArray));
end;
Is it me, or is that just really ugly (and codous, or what's the word for a lot of code for what it does). Since I really (really) hate to see long lists of overloads for all kinds of types, and really (really) like the Variant type, I've created jsonDoc, and it would look like this:
var
x:array of OleVariant;
i:integer;
begin
SetLength(x,AInputList.Count);
for i:=0 to AInputListCount-1 do
x[i]:=JSON(
['idrating',AInputList[i].IdRating
,'idmark',AInputList[i].IdMark
,'value',AInputList[i].Value
,'description',AInputList[i].Description
,'timeposted',FormatDateTime('yyyy-mm-dd hh:mm:ss', AInputList[i].TimePosted)//VarFromDateTime?
]);
end;
AResponse:=JSON(['ratings',VarArrayOf(x)]);
Found a workraround:
procedure ListToJson(AInputList: TList<TRating>;
AResponse: TJSONObject);
var
i: Integer;
jsonPair: TJSONPair;
jsonObject: TList<TJSONObject>;
jsonArray: TJSONArray;
begin
jsonArray := TJSONArray.Create();
jsonObject := TList<TJSONObject>.Create;
jsonPair := TJSONPair.Create('ratings', jsonArray);
try
for i := 0 to AInputList.Count - 1 do
begin
jsonObject.Add(TJSONObject.Create());
jsonObject.Last.AddPair(TJSONPair.Create('idrating', IntToStr(AInputList[i].IdRating)));
jsonObject.Last.AddPair(TJSONPair.Create('idmark', IntToStr(AInputList[i].IdMark)));
jsonObject.Last.AddPair(TJSONPair.Create('value', IntToStr(AInputList[i].Value)));
jsonObject.Last.AddPair(TJSONPair.Create('description', AInputList[i].Description));
jsonObject.Last.AddPair(TJSONPair.Create('timeposted', FormatDateTime('yyyy-mm-dd hh:mm:ss', AInputList[i].TimePosted)));
jsonArray.AddElement(jsonObject.Last);
end;
AResponse.AddPair(jsonPair);
finally
jsonObject.Free;
end;
end;

Read a specific value from a JSON request using SuperObject

Hello I get the next result in a web API in JSON format:
[
{
"$id":"47",
"CodISO":"BIH",
"ES":"Bosnia y Herzegovina",
"EN":"Bosnia and Herzegovina"
},
{
"$id":"48",
"CodISO":"BLR",
"ES":"Bielorrusia",
"EN":"Belarus"
},
{
"$id":"49",
"CodISO":"BLZ",
"ES":"Belice",
"EN":"Belize"
},
{
"$id":"50",
"CodISO":"BOL",
"ES":"Bolivia",
"EN":"Bolivia"
},
{
"$id":"51",
"CodISO":"BON",
"ES":"Bonaire",
"EN":"Bonaire"
},
{
"$id":"52",
"CodISO":"BOT",
"ES":"Botsuana",
"EN":"Botswana"
},
{
"$id":"53",
"CodISO":"BRA",
"ES":"Brasil",
"EN":"Brazil"
},
{
"$id":"54",
"CodISO":"BRB",
"ES":"Barbados",
"EN":"Barbados"
}
]
Now, I want read the value from item 'ES' where the value of item 'CodISO' = 'BOL' in Delphi SuperObject, I'm not able to find the solution, took all day trying it.
I don't know how iterate with SuperObject elements as I do it with Embarcadero TJSONValue, TJSONObject, TJSONArray. I'm a newbie with SuperObject:
var
json: ISuperObject;
Retriever: TIdHTTP;
Url: string;
AnsiStr: AnsiString;
begin
URL := Form1.RestClient1.BaseURL;
try
Retriever := TIdHTTP.Create(nil);
try
AnsiStr := Retriever.Get(Url);
json := SO(AnsiStr);
{ Here code to iterate with json elements in SuperObject.......
.
.
.
.
}
finally
Retriever.Free;
end;
except
on E: Exception do
ShowMessage(E.ClassName + ': ' + E.Message);
end;
End;
As Sir Rufo said, you need to read the SuperObject documentation.
Try something like this:
var
JsonArr, JsonObj: ISuperObject;
Retriever: TIdHTTP;
Url, JsonStr, ES: string;
I: Integer;
begin
URL := Form1.RestClient1.BaseURL;
try
Retriever := TIdHTTP.Create(nil);
try
JsonStr := Retriever.Get(Url);
finally
Retriever.Free;
end;
JsonArr := SO(JsonStr).AsArray;
for I := 0 to JsonArr.Length-1 do
begin
JsonObj := JsonArr.O[I];
if JsonObj.S['CodISO'] = 'BOL' then
begin
ES := JsonObj.S['ES'];
Break;
end;
end;
except
on E: Exception do
ShowMessage(E.ClassName + ': ' + E.Message);
end;
end;