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
(SQL Server) 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 } }
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls. -- CREATE PROCEDURE ChilkatSample AS BEGIN DECLARE @hr int DECLARE @iTmp0 int -- Important: Do not use nvarchar(max). See the warning about using nvarchar(max). DECLARE @sTmp0 nvarchar(4000) DECLARE @json int -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT IF @hr <> 0 BEGIN PRINT 'Failed to create ActiveX component' RETURN END EXEC sp_OASetProperty @json, 'EmitCompact', 0 -- Assume the file contains the data as shown above.. DECLARE @success int EXEC sp_OAMethod @json, 'LoadFile', @success OUT, 'qa_data/json/sample2.json' IF @success <> 1 BEGIN EXEC sp_OAGetProperty @json, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @json RETURN END -- First navigate to the "sampleData" object: DECLARE @sampleData int EXEC sp_OAMethod @json, 'ObjectOf', @sampleData OUT, 'sampleData' -- Demonstrate BoolAt and BoolOf EXEC sp_OAMethod @sampleData, 'BoolOf', @iTmp0 OUT, 'hungry' PRINT 'hungry: ' + @iTmp0 EXEC sp_OAMethod @sampleData, 'BoolAt', @iTmp0 OUT, 2 PRINT 'hungry: ' + @iTmp0 -- StringOf returns the value as a string regardless of it's actual type: EXEC sp_OAMethod @sampleData, 'StringOf', @sTmp0 OUT, 'pi' PRINT 'pi: ' + @sTmp0 EXEC sp_OAMethod @sampleData, 'StringOf', @sTmp0 OUT, 'answer' PRINT 'answer: ' + @sTmp0 EXEC sp_OAMethod @sampleData, 'StringOf', @sTmp0 OUT, 'withoutValue' PRINT 'withoutValue: ' + @sTmp0 EXEC sp_OAMethod @sampleData, 'StringOf', @sTmp0 OUT, 'hungry' PRINT 'hungry: ' + @sTmp0 -- Demonstrate IsNullOf / IsNullAt EXEC sp_OAMethod @sampleData, 'IsNullOf', @iTmp0 OUT, 'withoutValue' PRINT 'withoutValue is null? ' + @iTmp0 EXEC sp_OAMethod @sampleData, 'IsNullAt', @iTmp0 OUT, 3 PRINT 'withoutValue is null? ' + @iTmp0 EXEC sp_OAMethod @sampleData, 'IsNullOf', @iTmp0 OUT, 'apple' PRINT 'apple is null? ' + @iTmp0 EXEC sp_OAMethod @sampleData, 'IsNullAt', @iTmp0 OUT, 1 PRINT 'apple is null? ' + @iTmp0 -- IntOf EXEC sp_OAMethod @sampleData, 'IntOf', @iTmp0 OUT, 'answer' PRINT 'answer: ' + @iTmp0 -- SetNullAt, SetNullOf -- Set "pi" to null EXEC sp_OAMethod @sampleData, 'SetNullAt', @success OUT, 0 -- Set "answer" to null EXEC sp_OAMethod @sampleData, 'SetNullOf', @success OUT, 'answer' -- Show the changes: EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT PRINT @sTmp0 -- Restore pi and apple: EXEC sp_OAMethod @sampleData, 'SetNumberAt', @success OUT, 0, '3.14' EXEC sp_OAMethod @sampleData, 'SetNumberOf', @success OUT, 'answer', '42' -- Show the changes: EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT PRINT @sTmp0 -- Add a null value named "afterApple" just after "apple" EXEC sp_OAMethod @sampleData, 'AddNullAt', @success OUT, 2, 'afterApple' -- Add a boolean value just after "pi" EXEC sp_OAMethod @sampleData, 'AddBoolAt', @success OUT, 1, 'afterPi', 0 EXEC @hr = sp_OADestroy @sampleData -- Examine the changes.. EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @json END GO |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.