Sample code for 30+ languages & platforms
Delphi DLL

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 DLL Downloads

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

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
json: HCkJsonObject;
sampleData: HCkJsonObject;

begin
success := False;

json := CkJsonObject_Create();
CkJsonObject_putEmitCompact(json,False);

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

// Get the "sampleData" object:
sampleData := CkJsonObject_Create();
CkJsonObject_ObjectOf2(json,'sampleData',sampleData);

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

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

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

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

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

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

// Restore pi and apple:
success := CkJsonObject_SetNumberAt(sampleData,0,'3.14');
success := CkJsonObject_SetNumberOf(sampleData,'answer','42');

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

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

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

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

CkJsonObject_Dispose(json);
CkJsonObject_Dispose(sampleData);

end;