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
(Tcl) Modify Parts of JSON DocumentDemonstrates how to modify parts of a JSON document. This example uses the following JSON document: { "fruit": [ { "kind": "apple", "count": 24, "fresh": true, "extraInfo": null, "listA": [ "abc", 1, null, false ], "objectB": { "animal" : "monkey" } }, { "kind": "pear", "count": 18, "fresh": false, "extraInfo": null "listA": [ "xyz", 24, null, true ], "objectB": { "animal" : "lemur" } } ], "list" : [ "banana", 12, true, null, "orange", 12.5, { "ticker": "AAPL" }, [ 1, 2, 3, 4, 5 ] ], "alien" : true }
load ./chilkat.dll set json [new_CkJsonObject] # Load the JSON from a file. set success [CkJsonObject_LoadFile $json "qa_data/json/modifySample.json"] if {$success != 1} then { puts [CkJsonObject_lastErrorText $json] delete_CkJsonObject $json exit } # This example will not check for errors (i.e. null / false / 0 return values)... # Get the "list" array: # listA is a CkJsonArray set listA [CkJsonObject_ArrayOf $json "list"] # Modify values in the list. # Change banana to plantain set success [CkJsonArray_SetStringAt $listA 0 "plantain"] # Change 12 to 24 set success [CkJsonArray_SetIntAt $listA 1 24] # Change true to false set success [CkJsonArray_SetBoolAt $listA 2 0] # Is the 3rd item null? set bNull [CkJsonArray_IsNullAt $listA 3] # Change "orange" to 32. set success [CkJsonArray_SetIntAt $listA 4 32] # Change 12.5 to 31.2 set success [CkJsonArray_SetNumberAt $listA 5 "31.2"] # Replace the { "ticker" : "AAPL" } object with { "ticker" : "GOOG" } # Do this by deleting, then inserting a new object at the same location. set success [CkJsonArray_DeleteAt $listA 6] set success [CkJsonArray_AddObjectAt $listA 6] # tickerObj is a CkJsonObject set tickerObj [CkJsonArray_ObjectAt $listA 6] set success [CkJsonObject_AddStringAt $tickerObj -1 "ticker" "GOOG"] delete_CkJsonObject $tickerObj # Replace "[ 1, 2, 3, 4, 5 ]" with "[ "apple", 22, true, null, 1080.25 ]" set success [CkJsonArray_DeleteAt $listA 7] set success [CkJsonArray_AddArrayAt $listA 7] # aa is a CkJsonArray set aa [CkJsonArray_ArrayAt $listA 7] set success [CkJsonArray_AddStringAt $aa -1 "apple"] set success [CkJsonArray_AddIntAt $aa -1 22] set success [CkJsonArray_AddBoolAt $aa -1 1] set success [CkJsonArray_AddNullAt $aa -1] set success [CkJsonArray_AddNumberAt $aa -1 "1080.25"] delete_CkJsonArray $aa delete_CkJsonArray $listA # Get the "fruit" array # aFruit is a CkJsonArray set aFruit [CkJsonObject_ArrayAt $json 0] # Get the 1st element: # appleObj is a CkJsonObject set appleObj [CkJsonArray_ObjectAt $aFruit 0] # Modify values by member name: set success [CkJsonObject_SetStringOf $appleObj "fruit" "fuji_apple"] set success [CkJsonObject_SetIntOf $appleObj "count" 46] set success [CkJsonObject_SetBoolOf $appleObj "fresh" 0] set success [CkJsonObject_SetStringOf $appleObj "extraInfo" "developed by growers at the Tohoku Research Station in Fujisaki"] delete_CkJsonObject $appleObj # Modify values by index: # pearObj is a CkJsonObject set pearObj [CkJsonArray_ObjectAt $aFruit 1] set success [CkJsonObject_SetStringAt $pearObj 0 "bartlett_pear"] set success [CkJsonObject_SetIntAt $pearObj 1 12] set success [CkJsonObject_SetBoolAt $pearObj 2 0] set success [CkJsonObject_SetStringAt $pearObj 3 "harvested in late August to early September"] delete_CkJsonObject $pearObj delete_CkJsonArray $aFruit CkJsonObject_put_EmitCompact $json 0 puts [CkJsonObject_emit $json] delete_CkJsonObject $json |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.