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
(DataFlex) 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 }
Use ChilkatAx-win32.pkg Procedure Test Handle hoJson Boolean iSuccess Variant vListA Handle hoListA Boolean iBNull Variant vTickerObj Handle hoTickerObj Variant vAa Handle hoAa Variant vAFruit Handle hoAFruit Variant vAppleObj Handle hoAppleObj Variant vPearObj Handle hoPearObj String sTemp1 Get Create (RefClass(cComChilkatJsonObject)) To hoJson If (Not(IsComObjectCreated(hoJson))) Begin Send CreateComObject of hoJson End // Load the JSON from a file. Get ComLoadFile Of hoJson "qa_data/json/modifySample.json" To iSuccess If (iSuccess <> True) Begin Get ComLastErrorText Of hoJson To sTemp1 Showln sTemp1 Procedure_Return End // This example will not check for errors (i.e. null / false / 0 return values)... // Get the "list" array: Get ComArrayOf Of hoJson "list" To vListA If (IsComObject(vListA)) Begin Get Create (RefClass(cComChilkatJsonArray)) To hoListA Set pvComObject Of hoListA To vListA End // Modify values in the list. // Change banana to plantain Get ComSetStringAt Of hoListA 0 "plantain" To iSuccess // Change 12 to 24 Get ComSetIntAt Of hoListA 1 24 To iSuccess // Change true to false Get ComSetBoolAt Of hoListA 2 False To iSuccess // Is the 3rd item null? Get ComIsNullAt Of hoListA 3 To iBNull // Change "orange" to 32. Get ComSetIntAt Of hoListA 4 32 To iSuccess // Change 12.5 to 31.2 Get ComSetNumberAt Of hoListA 5 "31.2" To iSuccess // Replace the { "ticker" : "AAPL" } object with { "ticker" : "GOOG" } // Do this by deleting, then inserting a new object at the same location. Get ComDeleteAt Of hoListA 6 To iSuccess Get ComAddObjectAt Of hoListA 6 To iSuccess Get ComObjectAt Of hoListA 6 To vTickerObj If (IsComObject(vTickerObj)) Begin Get Create (RefClass(cComChilkatJsonObject)) To hoTickerObj Set pvComObject Of hoTickerObj To vTickerObj End Get ComAddStringAt Of hoTickerObj -1 "ticker" "GOOG" To iSuccess Send Destroy of hoTickerObj // Replace "[ 1, 2, 3, 4, 5 ]" with "[ "apple", 22, true, null, 1080.25 ]" Get ComDeleteAt Of hoListA 7 To iSuccess Get ComAddArrayAt Of hoListA 7 To iSuccess Get ComArrayAt Of hoListA 7 To vAa If (IsComObject(vAa)) Begin Get Create (RefClass(cComChilkatJsonArray)) To hoAa Set pvComObject Of hoAa To vAa End Get ComAddStringAt Of hoAa -1 "apple" To iSuccess Get ComAddIntAt Of hoAa -1 22 To iSuccess Get ComAddBoolAt Of hoAa -1 True To iSuccess Get ComAddNullAt Of hoAa -1 To iSuccess Get ComAddNumberAt Of hoAa -1 "1080.25" To iSuccess Send Destroy of hoAa Send Destroy of hoListA // Get the "fruit" array Get ComArrayAt Of hoJson 0 To vAFruit If (IsComObject(vAFruit)) Begin Get Create (RefClass(cComChilkatJsonArray)) To hoAFruit Set pvComObject Of hoAFruit To vAFruit End // Get the 1st element: Get ComObjectAt Of hoAFruit 0 To vAppleObj If (IsComObject(vAppleObj)) Begin Get Create (RefClass(cComChilkatJsonObject)) To hoAppleObj Set pvComObject Of hoAppleObj To vAppleObj End // Modify values by member name: Get ComSetStringOf Of hoAppleObj "fruit" "fuji_apple" To iSuccess Get ComSetIntOf Of hoAppleObj "count" 46 To iSuccess Get ComSetBoolOf Of hoAppleObj "fresh" False To iSuccess Get ComSetStringOf Of hoAppleObj "extraInfo" "developed by growers at the Tohoku Research Station in Fujisaki" To iSuccess Send Destroy of hoAppleObj // Modify values by index: Get ComObjectAt Of hoAFruit 1 To vPearObj If (IsComObject(vPearObj)) Begin Get Create (RefClass(cComChilkatJsonObject)) To hoPearObj Set pvComObject Of hoPearObj To vPearObj End Get ComSetStringAt Of hoPearObj 0 "bartlett_pear" To iSuccess Get ComSetIntAt Of hoPearObj 1 12 To iSuccess Get ComSetBoolAt Of hoPearObj 2 False To iSuccess Get ComSetStringAt Of hoPearObj 3 "harvested in late August to early September" To iSuccess Send Destroy of hoPearObj Send Destroy of hoAFruit Set ComEmitCompact Of hoJson To False Get ComEmit Of hoJson To sTemp1 Showln sTemp1 End_Procedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.