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) Create a JSON Array of ObjectsDemonstrates how to create a JSON array of objects.
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls. -- CREATE PROCEDURE ChilkatSample AS BEGIN DECLARE @hr int -- Important: Do not use nvarchar(max). See the warning about using nvarchar(max). DECLARE @sTmp0 nvarchar(4000) DECLARE @arr int -- Use "Chilkat_9_5_0.JsonArray" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.JsonArray', @arr OUT IF @hr <> 0 BEGIN PRINT 'Failed to create ActiveX component' RETURN END DECLARE @success int -- Add an empty object at the 1st JSON array position. EXEC sp_OAMethod @arr, 'AddObjectAt', @success OUT, 0 -- Get the object we just created. DECLARE @obj int EXEC sp_OAMethod @arr, 'ObjectAt', @obj OUT, 0 EXEC sp_OAMethod @obj, 'UpdateString', @success OUT, 'Name', 'Otto' EXEC sp_OAMethod @obj, 'UpdateInt', @success OUT, 'Age', 29 EXEC sp_OAMethod @obj, 'UpdateBool', @success OUT, 'Married', 0 EXEC @hr = sp_OADestroy @obj -- Add an empty object at the 2nd JSON array position. EXEC sp_OAMethod @arr, 'AddObjectAt', @success OUT, 1 EXEC sp_OAMethod @arr, 'ObjectAt', @obj OUT, 1 EXEC sp_OAMethod @obj, 'UpdateString', @success OUT, 'Name', 'Connor' EXEC sp_OAMethod @obj, 'UpdateInt', @success OUT, 'Age', 43 EXEC sp_OAMethod @obj, 'UpdateBool', @success OUT, 'Married', 1 EXEC @hr = sp_OADestroy @obj -- Add an empty object at the 3rd JSON array position. EXEC sp_OAMethod @arr, 'AddObjectAt', @success OUT, 2 EXEC sp_OAMethod @arr, 'ObjectAt', @obj OUT, 2 EXEC sp_OAMethod @obj, 'UpdateString', @success OUT, 'Name', 'Ramona' EXEC sp_OAMethod @obj, 'UpdateInt', @success OUT, 'Age', 34 EXEC sp_OAMethod @obj, 'UpdateBool', @success OUT, 'Married', 1 EXEC @hr = sp_OADestroy @obj -- Examine what we have: EXEC sp_OASetProperty @arr, 'EmitCompact', 0 EXEC sp_OAMethod @arr, 'Emit', @sTmp0 OUT PRINT @sTmp0 -- The output is: -- [ -- { -- "Name": "Otto", -- "Age": 29, -- "Married": false -- }, -- { -- "Name": "Connor", -- "Age": 43, -- "Married": true -- }, -- { -- "Name": "Ramona", -- "Age": 34, -- "Married": true -- } -- ] EXEC @hr = sp_OADestroy @arr END GO |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.