Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Modify Parts of JSON Document
See more JSON Examples
Demonstrates how to modify parts of a JSON document. This example uses the following JSON document:
{
"fruit": [
{
"kind": "apple",
"count": 24,
"fresh": true,
"extraInfo": null,
"listA": [ "abc", 1, null, false ],
"objectB": { "animal" : "monkey" }
},
{
"kind": "pear",
"count": 18,
"fresh": false,
"extraInfo": null
"listA": [ "xyz", 24, null, true ],
"objectB": { "animal" : "lemur" }
}
],
"list" : [ "banana", 12, true, null, "orange", 12.5, { "ticker": "AAPL" }, [ 1, 2, 3, 4, 5 ] ],
"alien" : true
}
Chilkat Pascal (Lazarus/Delphi) Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.JsonArray,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
json: TJsonObject;
listA: TJsonArray;
bNull: Boolean;
tickerObj: TJsonObject;
aa: TJsonArray;
aFruit: TJsonArray;
appleObj: TJsonObject;
pearObj: TJsonObject;
begin
success := False;
json := TJsonObject.Create;
// Load the JSON from a file.
success := json.LoadFile('qa_data/json/modifySample.json');
if (success = False) then
begin
WriteLn(json.LastErrorText);
Exit;
end;
// This example will not check for errors (i.e. null / false / 0 return values)...
// Get the "list" array:
listA := TJsonArray.Create;
json.ArrayOf2('list',listA);
// Modify values in the list.
// Change banana to plantain
success := listA.SetStringAt(0,'plantain');
// Change 12 to 24
success := listA.SetIntAt(1,24);
// Change true to false
success := listA.SetBoolAt(2,False);
// Is the 3rd item null?
bNull := listA.IsNullAt(3);
// Change "orange" to 32.
success := listA.SetIntAt(4,32);
// Change 12.5 to 31.2
success := listA.SetNumberAt(5,'31.2');
// Replace the { "ticker" : "AAPL" } object with { "ticker" : "GOOG" }
// Do this by deleting, then inserting a new object at the same location.
success := listA.DeleteAt(6);
tickerObj := TJsonObject.Create;
listA.AddObjectAt2(6,tickerObj);
success := tickerObj.AppendString('ticker','GOOG');
// Replace "[ 1, 2, 3, 4, 5 ]" with "[ "apple", 22, true, null, 1080.25 ]"
success := listA.DeleteAt(7);
aa := TJsonArray.Create;
listA.AddArrayAt2(7,aa);
success := aa.AddStringAt(-1,'apple');
success := aa.AddIntAt(-1,22);
success := aa.AddBoolAt(-1,True);
success := aa.AddNullAt(-1);
success := aa.AddNumberAt(-1,'1080.25');
// Get the "fruit" array
aFruit := TJsonArray.Create;
json.ArrayAt2(0,aFruit);
// Get the 1st element:
appleObj := TJsonObject.Create;
aFruit.ObjectAt2(0,appleObj);
// Modify values by member name:
success := appleObj.SetStringOf('fruit','fuji_apple');
success := appleObj.SetIntOf('count',46);
success := appleObj.SetBoolOf('fresh',False);
success := appleObj.SetStringOf('extraInfo','developed by growers at the Tohoku Research Station in Fujisaki');
// Modify values by index:
pearObj := TJsonObject.Create;
aFruit.ObjectAt2(1,pearObj);
success := pearObj.SetStringAt(0,'bartlett_pear');
success := pearObj.SetIntAt(1,12);
success := pearObj.SetBoolAt(2,False);
success := pearObj.SetStringAt(3,'harvested in late August to early September');
json.EmitCompact := False;
WriteLn(json.Emit());
json.Free;
listA.Free;
tickerObj.Free;
aa.Free;
aFruit.Free;
appleObj.Free;
pearObj.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.