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
(PureBasic) 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 } }
IncludeFile "CkJsonObject.pb" Procedure ChilkatExample() json.i = CkJsonObject::ckCreate() If json.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkJsonObject::setCkEmitCompact(json, 0) ; Assume the file contains the data as shown above.. success.i = CkJsonObject::ckLoadFile(json,"qa_data/json/sample2.json") If success <> 1 Debug CkJsonObject::ckLastErrorText(json) CkJsonObject::ckDispose(json) ProcedureReturn EndIf ; First navigate to the "sampleData" object: sampleData.i = CkJsonObject::ckObjectOf(json,"sampleData") ; Demonstrate BoolAt and BoolOf Debug "hungry: " + Str(CkJsonObject::ckBoolOf(sampleData,"hungry")) Debug "hungry: " + Str(CkJsonObject::ckBoolAt(sampleData,2)) ; StringOf returns the value as a string regardless of it's actual type: Debug "pi: " + CkJsonObject::ckStringOf(sampleData,"pi") Debug "answer: " + CkJsonObject::ckStringOf(sampleData,"answer") Debug "withoutValue: " + CkJsonObject::ckStringOf(sampleData,"withoutValue") Debug "hungry: " + CkJsonObject::ckStringOf(sampleData,"hungry") ; Demonstrate IsNullOf / IsNullAt Debug "withoutValue is null? " + Str(CkJsonObject::ckIsNullOf(sampleData,"withoutValue")) Debug "withoutValue is null? " + Str(CkJsonObject::ckIsNullAt(sampleData,3)) Debug "apple is null? " + Str(CkJsonObject::ckIsNullOf(sampleData,"apple")) Debug "apple is null? " + Str(CkJsonObject::ckIsNullAt(sampleData,1)) ; IntOf Debug "answer: " + Str(CkJsonObject::ckIntOf(sampleData,"answer")) ; SetNullAt, SetNullOf ; Set "pi" to null success = CkJsonObject::ckSetNullAt(sampleData,0) ; Set "answer" to null success = CkJsonObject::ckSetNullOf(sampleData,"answer") ; Show the changes: Debug CkJsonObject::ckEmit(json) ; Restore pi and apple: success = CkJsonObject::ckSetNumberAt(sampleData,0,"3.14") success = CkJsonObject::ckSetNumberOf(sampleData,"answer","42") ; Show the changes: Debug CkJsonObject::ckEmit(json) ; Add a null value named "afterApple" just after "apple" success = CkJsonObject::ckAddNullAt(sampleData,2,"afterApple") ; Add a boolean value just after "pi" success = CkJsonObject::ckAddBoolAt(sampleData,1,"afterPi",0) CkJsonObject::ckDispose(sampleData) ; Examine the changes.. Debug CkJsonObject::ckEmit(json) CkJsonObject::ckDispose(json) ProcedureReturn EndProcedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.