Sample code for 30+ languages & platforms
Delphi ActiveX

JSON: Miscellaneous Operations

See more JSON Examples

Demonstrates a variety of JSON API methods. This example uses the following JSON document:
{
   "alphabet": "abcdefghijklmnopqrstuvwxyz",
   "sampleData" : {
           "pi": 3.14,
	   "apple": "juicy",
	   "hungry": true,
	   "withoutValue": null,
           "answer": 42
          
	}
}

Chilkat Delphi ActiveX Downloads

Delphi ActiveX
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Chilkat_TLB;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Integer;
json: TChilkatJsonObject;
sampleData: TChilkatJsonObject;

begin
success := 0;

json := TChilkatJsonObject.Create(Self);
json.EmitCompact := 0;

// Assume the file contains the data as shown above..
success := json.LoadFile('qa_data/json/sample2.json');
if (success = 0) then
  begin
    Memo1.Lines.Add(json.LastErrorText);
    Exit;
  end;

// Get the "sampleData" object:
sampleData := TChilkatJsonObject.Create(Self);
json.ObjectOf2('sampleData',sampleData.ControlInterface);

// Demonstrate BoolAt and BoolOf
Memo1.Lines.Add('hungry: ' + IntToStr(Ord(sampleData.BoolOf('hungry'))));
Memo1.Lines.Add('hungry: ' + IntToStr(Ord(sampleData.BoolAt(2))));

// StringOf returns the value as a string regardless of it's actual type:
Memo1.Lines.Add('pi: ' + sampleData.StringOf('pi'));
Memo1.Lines.Add('answer: ' + sampleData.StringOf('answer'));
Memo1.Lines.Add('withoutValue: ' + sampleData.StringOf('withoutValue'));
Memo1.Lines.Add('hungry: ' + sampleData.StringOf('hungry'));

// Demonstrate IsNullOf / IsNullAt
Memo1.Lines.Add('withoutValue is null? ' + IntToStr(Ord(sampleData.IsNullOf('withoutValue'))));
Memo1.Lines.Add('withoutValue is null? ' + IntToStr(Ord(sampleData.IsNullAt(3))));
Memo1.Lines.Add('apple is null? ' + IntToStr(Ord(sampleData.IsNullOf('apple'))));
Memo1.Lines.Add('apple is null? ' + IntToStr(Ord(sampleData.IsNullAt(1))));

// IntOf
Memo1.Lines.Add('answer: ' + IntToStr(sampleData.IntOf('answer')));

// SetNullAt, SetNullOf
// Set "pi" to null
success := sampleData.SetNullAt(0);
// Set "answer" to null
success := sampleData.SetNullOf('answer');

// Show the changes:
Memo1.Lines.Add(json.Emit());

// Restore pi and apple:
success := sampleData.SetNumberAt(0,'3.14');
success := sampleData.SetNumberOf('answer','42');

// Show the changes:
Memo1.Lines.Add(json.Emit());

// Add a null value named "afterApple" just after "apple"
success := sampleData.AddNullAt(2,'afterApple');

// Add a boolean value just after "pi"
success := sampleData.AddBoolAt(1,'afterPi',0);

// Examine the changes..
Memo1.Lines.Add(json.Emit());
end;