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
(PureBasic) 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 }
IncludeFile "CkJsonArray.pb" IncludeFile "CkJsonObject.pb" Procedure ChilkatExample() json.i = CkJsonObject::ckCreate() If json.i = 0 Debug "Failed to create object." ProcedureReturn EndIf ; Load the JSON from a file. success.i = CkJsonObject::ckLoadFile(json,"qa_data/json/modifySample.json") If success <> 1 Debug CkJsonObject::ckLastErrorText(json) CkJsonObject::ckDispose(json) ProcedureReturn EndIf ; This example will not check for errors (i.e. null / false / 0 return values)... ; Get the "list" array: listA.i = CkJsonObject::ckArrayOf(json,"list") ; Modify values in the list. ; Change banana to plantain success = CkJsonArray::ckSetStringAt(listA,0,"plantain") ; Change 12 to 24 success = CkJsonArray::ckSetIntAt(listA,1,24) ; Change true to false success = CkJsonArray::ckSetBoolAt(listA,2,0) ; Is the 3rd item null? bNull.i = CkJsonArray::ckIsNullAt(listA,3) ; Change "orange" to 32. success = CkJsonArray::ckSetIntAt(listA,4,32) ; Change 12.5 to 31.2 success = CkJsonArray::ckSetNumberAt(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. success = CkJsonArray::ckDeleteAt(listA,6) success = CkJsonArray::ckAddObjectAt(listA,6) tickerObj.i = CkJsonArray::ckObjectAt(listA,6) success = CkJsonObject::ckAddStringAt(tickerObj,-1,"ticker","GOOG") CkJsonObject::ckDispose(tickerObj) ; Replace "[ 1, 2, 3, 4, 5 ]" with "[ "apple", 22, true, null, 1080.25 ]" success = CkJsonArray::ckDeleteAt(listA,7) success = CkJsonArray::ckAddArrayAt(listA,7) aa.i = CkJsonArray::ckArrayAt(listA,7) success = CkJsonArray::ckAddStringAt(aa,-1,"apple") success = CkJsonArray::ckAddIntAt(aa,-1,22) success = CkJsonArray::ckAddBoolAt(aa,-1,1) success = CkJsonArray::ckAddNullAt(aa,-1) success = CkJsonArray::ckAddNumberAt(aa,-1,"1080.25") CkJsonArray::ckDispose(aa) CkJsonArray::ckDispose(listA) ; Get the "fruit" array aFruit.i = CkJsonObject::ckArrayAt(json,0) ; Get the 1st element: appleObj.i = CkJsonArray::ckObjectAt(aFruit,0) ; Modify values by member name: success = CkJsonObject::ckSetStringOf(appleObj,"fruit","fuji_apple") success = CkJsonObject::ckSetIntOf(appleObj,"count",46) success = CkJsonObject::ckSetBoolOf(appleObj,"fresh",0) success = CkJsonObject::ckSetStringOf(appleObj,"extraInfo","developed by growers at the Tohoku Research Station in Fujisaki") CkJsonObject::ckDispose(appleObj) ; Modify values by index: pearObj.i = CkJsonArray::ckObjectAt(aFruit,1) success = CkJsonObject::ckSetStringAt(pearObj,0,"bartlett_pear") success = CkJsonObject::ckSetIntAt(pearObj,1,12) success = CkJsonObject::ckSetBoolAt(pearObj,2,0) success = CkJsonObject::ckSetStringAt(pearObj,3,"harvested in late August to early September") CkJsonObject::ckDispose(pearObj) CkJsonArray::ckDispose(aFruit) CkJsonObject::setCkEmitCompact(json, 0) Debug CkJsonObject::ckEmit(json) CkJsonObject::ckDispose(json) ProcedureReturn EndProcedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.