Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Delphi DLL) JSON: Miscellaneous OperationsDemonstrates 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 } }
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 json: HCkJsonObject; success: Boolean; sampleData: HCkJsonObject; begin 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 <> True) then begin Memo1.Lines.Add(CkJsonObject__lastErrorText(json)); Exit; end; // First navigate to the "sampleData" object: sampleData := CkJsonObject_ObjectOf(json,'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); CkJsonObject_Dispose(sampleData); // Examine the changes.. Memo1.Lines.Add(CkJsonObject__emit(json)); CkJsonObject_Dispose(json); end; |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.