Chilkat HOME Android™ Classic ASP C C++ C# Mono C# .NET Core C# C# UWP/WinRT DataFlex Delphi ActiveX Delphi DLL Visual FoxPro Java Lianja MFC Objective-C Perl PHP ActiveX PHP Extension PowerBuilder PowerShell PureBasic CkPython Chilkat2-Python Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ Visual Basic 6.0 VB.NET VB.NET UWP/WinRT VBScript Xojo Plugin Node.js Excel Go
(MFC) JSON: Miscellaneous OperationsDemonstrates a variety of JSON API methods. This example uses the following JSON document: { "alphabet": "abcdefghijklmnopqrstuvwxyz", "sampleData" : { "pi": 3.14, "apple": "juicy", "hungry": true, "withoutValue": null, "answer": 42 } }
#include <CkJsonObject.h> void ChilkatSample(void) { CkString strOut; CkJsonObject json; json.put_EmitCompact(false); // Assume the file contains the data as shown above.. bool success = json.LoadFile("qa_data/json/sample2.json"); if (success != true) { strOut.append(json.lastErrorText()); strOut.append("\r\n"); SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); return; } // First navigate to the "sampleData" object: CkJsonObject *sampleData = json.ObjectOf("sampleData"); // Demonstrate BoolAt and BoolOf strOut.append("hungry: "); strOut.appendInt(sampleData->BoolOf("hungry")); strOut.append("\r\n"); strOut.append("hungry: "); strOut.appendInt(sampleData->BoolAt(2)); strOut.append("\r\n"); // StringOf returns the value as a string regardless of it's actual type: strOut.append("pi: "); strOut.append(sampleData->stringOf("pi")); strOut.append("\r\n"); strOut.append("answer: "); strOut.append(sampleData->stringOf("answer")); strOut.append("\r\n"); strOut.append("withoutValue: "); strOut.append(sampleData->stringOf("withoutValue")); strOut.append("\r\n"); strOut.append("hungry: "); strOut.append(sampleData->stringOf("hungry")); strOut.append("\r\n"); // Demonstrate IsNullOf / IsNullAt strOut.append("withoutValue is null? "); strOut.appendInt(sampleData->IsNullOf("withoutValue")); strOut.append("\r\n"); strOut.append("withoutValue is null? "); strOut.appendInt(sampleData->IsNullAt(3)); strOut.append("\r\n"); strOut.append("apple is null? "); strOut.appendInt(sampleData->IsNullOf("apple")); strOut.append("\r\n"); strOut.append("apple is null? "); strOut.appendInt(sampleData->IsNullAt(1)); strOut.append("\r\n"); // IntOf strOut.append("answer: "); strOut.appendInt(sampleData->IntOf("answer")); strOut.append("\r\n"); // SetNullAt, SetNullOf // Set "pi" to null success = sampleData->SetNullAt(0); // Set "answer" to null success = sampleData->SetNullOf("answer"); // Show the changes: strOut.append(json.emit()); strOut.append("\r\n"); // Restore pi and apple: success = sampleData->SetNumberAt(0,"3.14"); success = sampleData->SetNumberOf("answer","42"); // Show the changes: strOut.append(json.emit()); strOut.append("\r\n"); // Add a null value named "afterApple" just after "apple" success = sampleData->AddNullAt(2,"afterApple"); // Add a boolean value just after "pi" success = sampleData->AddBoolAt(1,"afterPi",false); delete sampleData; // Examine the changes.. strOut.append(json.emit()); strOut.append("\r\n"); SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); } |
© 2000-2022 Chilkat Software, Inc. All Rights Reserved.