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 Iterate MembersDemonstrates how to loop over the immediate members of a JSON object.
-- 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 @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 DECLARE @jsonStr nvarchar(4000) SELECT @jsonStr = '{ "id": 1, "name": "A green door", "tags": ["home", "green"], "price": 125 }' DECLARE @success int EXEC sp_OAMethod @json, 'Load', @success OUT, @jsonStr IF @success <> 1 BEGIN EXEC sp_OAGetProperty @json, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @json RETURN END DECLARE @numMembers int EXEC sp_OAGetProperty @json, 'Size', @numMembers OUT DECLARE @i int SELECT @i = 0 WHILE @i <= @numMembers - 1 BEGIN DECLARE @name nvarchar(4000) EXEC sp_OAMethod @json, 'NameAt', @name OUT, @i DECLARE @value nvarchar(4000) EXEC sp_OAMethod @json, 'StringAt', @value OUT, @i PRINT @name + ': ' + @value DECLARE @iValue int EXEC sp_OAMethod @json, 'IntAt', @iValue OUT, @i PRINT @name + ' as integer: ' + @iValue SELECT @i = @i + 1 END -- Note: The StringAt method returns the value as a string regardless of the type. -- If the value is a JSON array (such as for ["home", "green"]), then the JSON encoding -- of the entire array is returned. -- The IntAt method returns the value as an integer. If the value does not convert to -- an integer, then 0 is returned EXEC @hr = sp_OADestroy @json END GO |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.