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
(Unicode C++) 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 }
#include <CkJsonObjectW.h> #include <CkJsonArrayW.h> void ChilkatSample(void) { CkJsonObjectW json; // Load the JSON from a file. bool success = json.LoadFile(L"qa_data/json/modifySample.json"); if (success != true) { wprintf(L"%s\n",json.lastErrorText()); return; } // This example will not check for errors (i.e. null / false / 0 return values)... // Get the "list" array: CkJsonArrayW *listA = json.ArrayOf(L"list"); // Modify values in the list. // Change banana to plantain success = listA->SetStringAt(0,L"plantain"); // Change 12 to 24 success = listA->SetIntAt(1,24); // Change true to false success = listA->SetBoolAt(2,false); // Is the 3rd item null? bool bNull = listA->IsNullAt(3); // Change "orange" to 32. success = listA->SetIntAt(4,32); // Change 12.5 to 31.2 success = listA->SetNumberAt(5,L"31.2"); // Replace the { "ticker" : "AAPL" } object with { "ticker" : "GOOG" } // Do this by deleting, then inserting a new object at the same location. success = listA->DeleteAt(6); success = listA->AddObjectAt(6); CkJsonObjectW *tickerObj = listA->ObjectAt(6); success = tickerObj->AddStringAt(-1,L"ticker",L"GOOG"); delete tickerObj; // Replace "[ 1, 2, 3, 4, 5 ]" with "[ "apple", 22, true, null, 1080.25 ]" success = listA->DeleteAt(7); success = listA->AddArrayAt(7); CkJsonArrayW *aa = listA->ArrayAt(7); success = aa->AddStringAt(-1,L"apple"); success = aa->AddIntAt(-1,22); success = aa->AddBoolAt(-1,true); success = aa->AddNullAt(-1); success = aa->AddNumberAt(-1,L"1080.25"); delete aa; delete listA; // Get the "fruit" array CkJsonArrayW *aFruit = json.ArrayAt(0); // Get the 1st element: CkJsonObjectW *appleObj = aFruit->ObjectAt(0); // Modify values by member name: success = appleObj->SetStringOf(L"fruit",L"fuji_apple"); success = appleObj->SetIntOf(L"count",46); success = appleObj->SetBoolOf(L"fresh",false); success = appleObj->SetStringOf(L"extraInfo",L"developed by growers at the Tohoku Research Station in Fujisaki"); delete appleObj; // Modify values by index: CkJsonObjectW *pearObj = aFruit->ObjectAt(1); success = pearObj->SetStringAt(0,L"bartlett_pear"); success = pearObj->SetIntAt(1,12); success = pearObj->SetBoolAt(2,false); success = pearObj->SetStringAt(3,L"harvested in late August to early September"); delete pearObj; delete aFruit; json.put_EmitCompact(false); wprintf(L"%s\n",json.emit()); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.