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
(Delphi DLL) 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 }
uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, JsonArray, JsonObject; ... procedure TForm1.Button1Click(Sender: TObject); var json: HCkJsonObject; success: Boolean; listA: HCkJsonArray; bNull: Boolean; tickerObj: HCkJsonObject; aa: HCkJsonArray; aFruit: HCkJsonArray; appleObj: HCkJsonObject; pearObj: HCkJsonObject; begin json := CkJsonObject_Create(); // Load the JSON from a file. success := CkJsonObject_LoadFile(json,'qa_data/json/modifySample.json'); if (success <> True) then begin Memo1.Lines.Add(CkJsonObject__lastErrorText(json)); Exit; end; // This example will not check for errors (i.e. null / false / 0 return values)... // Get the "list" array: listA := CkJsonObject_ArrayOf(json,'list'); // Modify values in the list. // Change banana to plantain success := CkJsonArray_SetStringAt(listA,0,'plantain'); // Change 12 to 24 success := CkJsonArray_SetIntAt(listA,1,24); // Change true to false success := CkJsonArray_SetBoolAt(listA,2,False); // Is the 3rd item null? bNull := CkJsonArray_IsNullAt(listA,3); // Change "orange" to 32. success := CkJsonArray_SetIntAt(listA,4,32); // Change 12.5 to 31.2 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. success := CkJsonArray_DeleteAt(listA,6); success := CkJsonArray_AddObjectAt(listA,6); tickerObj := CkJsonArray_ObjectAt(listA,6); success := CkJsonObject_AddStringAt(tickerObj,-1,'ticker','GOOG'); CkJsonObject_Dispose(tickerObj); // Replace "[ 1, 2, 3, 4, 5 ]" with "[ "apple", 22, true, null, 1080.25 ]" success := CkJsonArray_DeleteAt(listA,7); success := CkJsonArray_AddArrayAt(listA,7); aa := CkJsonArray_ArrayAt(listA,7); success := CkJsonArray_AddStringAt(aa,-1,'apple'); success := CkJsonArray_AddIntAt(aa,-1,22); success := CkJsonArray_AddBoolAt(aa,-1,True); success := CkJsonArray_AddNullAt(aa,-1); success := CkJsonArray_AddNumberAt(aa,-1,'1080.25'); CkJsonArray_Dispose(aa); CkJsonArray_Dispose(listA); // Get the "fruit" array aFruit := CkJsonObject_ArrayAt(json,0); // Get the 1st element: appleObj := CkJsonArray_ObjectAt(aFruit,0); // Modify values by member name: success := CkJsonObject_SetStringOf(appleObj,'fruit','fuji_apple'); success := CkJsonObject_SetIntOf(appleObj,'count',46); success := CkJsonObject_SetBoolOf(appleObj,'fresh',False); success := CkJsonObject_SetStringOf(appleObj,'extraInfo','developed by growers at the Tohoku Research Station in Fujisaki'); CkJsonObject_Dispose(appleObj); // Modify values by index: pearObj := CkJsonArray_ObjectAt(aFruit,1); success := CkJsonObject_SetStringAt(pearObj,0,'bartlett_pear'); success := CkJsonObject_SetIntAt(pearObj,1,12); success := CkJsonObject_SetBoolAt(pearObj,2,False); success := CkJsonObject_SetStringAt(pearObj,3,'harvested in late August to early September'); CkJsonObject_Dispose(pearObj); CkJsonArray_Dispose(aFruit); CkJsonObject_putEmitCompact(json,False); Memo1.Lines.Add(CkJsonObject__emit(json)); CkJsonObject_Dispose(json); end; |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.