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
(AutoIt) 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 }
$oJson = ObjCreate("Chilkat.JsonObject") ; Load the JSON from a file. Local $bSuccess = $oJson.LoadFile("qa_data/json/modifySample.json") If ($bSuccess <> True) Then ConsoleWrite($oJson.LastErrorText & @CRLF) Exit EndIf ; This example will not check for errors (i.e. null / false / 0 return values)... ; Get the "list" array: Local $oListA = $oJson.ArrayOf("list") ; Modify values in the list. ; Change banana to plantain $bSuccess = $oListA.SetStringAt(0,"plantain") ; Change 12 to 24 $bSuccess = $oListA.SetIntAt(1,24) ; Change true to false $bSuccess = $oListA.SetBoolAt(2,False) ; Is the 3rd item null? Local $bNull = $oListA.IsNullAt(3) ; Change "orange" to 32. $bSuccess = $oListA.SetIntAt(4,32) ; Change 12.5 to 31.2 $bSuccess = $oListA.SetNumberAt(5,"31.2") ; Replace the { "ticker" : "AAPL" } object with { "ticker" : "GOOG" } ; Do this by deleting, then inserting a new object at the same location. $bSuccess = $oListA.DeleteAt(6) $bSuccess = $oListA.AddObjectAt(6) Local $oTickerObj = $oListA.ObjectAt(6) $bSuccess = $oTickerObj.AddStringAt(-1,"ticker","GOOG") ; Replace "[ 1, 2, 3, 4, 5 ]" with "[ "apple", 22, true, null, 1080.25 ]" $bSuccess = $oListA.DeleteAt(7) $bSuccess = $oListA.AddArrayAt(7) Local $oAa = $oListA.ArrayAt(7) $bSuccess = $oAa.AddStringAt(-1,"apple") $bSuccess = $oAa.AddIntAt(-1,22) $bSuccess = $oAa.AddBoolAt(-1,True) $bSuccess = $oAa.AddNullAt(-1) $bSuccess = $oAa.AddNumberAt(-1,"1080.25") ; Get the "fruit" array Local $oAFruit = $oJson.ArrayAt(0) ; Get the 1st element: Local $oAppleObj = $oAFruit.ObjectAt(0) ; Modify values by member name: $bSuccess = $oAppleObj.SetStringOf("fruit","fuji_apple") $bSuccess = $oAppleObj.SetIntOf("count",46) $bSuccess = $oAppleObj.SetBoolOf("fresh",False) $bSuccess = $oAppleObj.SetStringOf("extraInfo","developed by growers at the Tohoku Research Station in Fujisaki") ; Modify values by index: Local $oPearObj = $oAFruit.ObjectAt(1) $bSuccess = $oPearObj.SetStringAt(0,"bartlett_pear") $bSuccess = $oPearObj.SetIntAt(1,12) $bSuccess = $oPearObj.SetBoolAt(2,False) $bSuccess = $oPearObj.SetStringAt(3,"harvested in late August to early September") $oJson.EmitCompact = False ConsoleWrite($oJson.Emit() & @CRLF) |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.