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 <C_CkJsonObjectW.h> #include <C_CkJsonArrayW.h> void ChilkatSample(void) { HCkJsonObjectW json; BOOL success; HCkJsonArrayW listA; BOOL bNull; HCkJsonObjectW tickerObj; HCkJsonArrayW aa; HCkJsonArrayW aFruit; HCkJsonObjectW appleObj; HCkJsonObjectW pearObj; json = CkJsonObjectW_Create(); // Load the JSON from a file. success = CkJsonObjectW_LoadFile(json,L"qa_data/json/modifySample.json"); if (success != TRUE) { wprintf(L"%s\n",CkJsonObjectW_lastErrorText(json)); CkJsonObjectW_Dispose(json); return; } // This example will not check for errors (i.e. null / false / 0 return values)... // Get the "list" array: listA = CkJsonObjectW_ArrayOf(json,L"list"); // Modify values in the list. // Change banana to plantain success = CkJsonArrayW_SetStringAt(listA,0,L"plantain"); // Change 12 to 24 success = CkJsonArrayW_SetIntAt(listA,1,24); // Change true to false success = CkJsonArrayW_SetBoolAt(listA,2,FALSE); // Is the 3rd item null? bNull = CkJsonArrayW_IsNullAt(listA,3); // Change "orange" to 32. success = CkJsonArrayW_SetIntAt(listA,4,32); // Change 12.5 to 31.2 success = CkJsonArrayW_SetNumberAt(listA,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 = CkJsonArrayW_DeleteAt(listA,6); success = CkJsonArrayW_AddObjectAt(listA,6); tickerObj = CkJsonArrayW_ObjectAt(listA,6); success = CkJsonObjectW_AddStringAt(tickerObj,-1,L"ticker",L"GOOG"); CkJsonObjectW_Dispose(tickerObj); // Replace "[ 1, 2, 3, 4, 5 ]" with "[ "apple", 22, true, null, 1080.25 ]" success = CkJsonArrayW_DeleteAt(listA,7); success = CkJsonArrayW_AddArrayAt(listA,7); aa = CkJsonArrayW_ArrayAt(listA,7); success = CkJsonArrayW_AddStringAt(aa,-1,L"apple"); success = CkJsonArrayW_AddIntAt(aa,-1,22); success = CkJsonArrayW_AddBoolAt(aa,-1,TRUE); success = CkJsonArrayW_AddNullAt(aa,-1); success = CkJsonArrayW_AddNumberAt(aa,-1,L"1080.25"); CkJsonArrayW_Dispose(aa); CkJsonArrayW_Dispose(listA); // Get the "fruit" array aFruit = CkJsonObjectW_ArrayAt(json,0); // Get the 1st element: appleObj = CkJsonArrayW_ObjectAt(aFruit,0); // Modify values by member name: success = CkJsonObjectW_SetStringOf(appleObj,L"fruit",L"fuji_apple"); success = CkJsonObjectW_SetIntOf(appleObj,L"count",46); success = CkJsonObjectW_SetBoolOf(appleObj,L"fresh",FALSE); success = CkJsonObjectW_SetStringOf(appleObj,L"extraInfo",L"developed by growers at the Tohoku Research Station in Fujisaki"); CkJsonObjectW_Dispose(appleObj); // Modify values by index: pearObj = CkJsonArrayW_ObjectAt(aFruit,1); success = CkJsonObjectW_SetStringAt(pearObj,0,L"bartlett_pear"); success = CkJsonObjectW_SetIntAt(pearObj,1,12); success = CkJsonObjectW_SetBoolAt(pearObj,2,FALSE); success = CkJsonObjectW_SetStringAt(pearObj,3,L"harvested in late August to early September"); CkJsonObjectW_Dispose(pearObj); CkJsonArrayW_Dispose(aFruit); CkJsonObjectW_putEmitCompact(json,FALSE); wprintf(L"%s\n",CkJsonObjectW_emit(json)); CkJsonObjectW_Dispose(json); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.