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) JSON: Array of ObjectsHere we have a JSON object that contains an array, where each element in the array is a JSON object. This example demonstrates how to access the objects contained within an array. { "employees":[ {"firstName":"John", "lastName":"Doe"}, {"firstName":"Anna", "lastName":"Smith"}, {"firstName":"Peter","lastName":"Jones"} ] }
IncludeFile "CkJsonArray.pb" IncludeFile "CkJsonObject.pb" Procedure ChilkatExample() json.i = CkJsonObject::ckCreate() If json.i = 0 Debug "Failed to create object." ProcedureReturn EndIf ; This is the above JSON with whitespace chars removed (SPACE, TAB, CR, and LF chars). ; The presence of whitespace chars for pretty-printing makes no difference to the Load ; method. jsonStr.s = "{" + Chr(34) + "employees" + Chr(34) + ":[{" + Chr(34) + "firstName" + Chr(34) + ":" + Chr(34) + "John" + Chr(34) + ", " + Chr(34) + "lastName" + Chr(34) + ":" + Chr(34) + "Doe" + Chr(34) + "},{" + Chr(34) + "firstName" + Chr(34) + ":" + Chr(34) + "Anna" + Chr(34) + ", " + Chr(34) + "lastName" + Chr(34) + ":" + Chr(34) + "Smith" + Chr(34) + "},{" + Chr(34) + "firstName" + Chr(34) + ":" + Chr(34) + "Peter" + Chr(34) + "," + Chr(34) + "lastName" + Chr(34) + ":" + Chr(34) + "Jones" + Chr(34) + "}]}" success.i = CkJsonObject::ckLoad(json,jsonStr) If success <> 1 Debug CkJsonObject::ckLastErrorText(json) CkJsonObject::ckDispose(json) ProcedureReturn EndIf ; Get the "employees" array. employees.i = CkJsonObject::ckArrayOf(json,"employees") If CkJsonObject::ckLastMethodSuccess(json) = 0 Debug "employees member not found." CkJsonObject::ckDispose(json) ProcedureReturn EndIf ; Iterate over each employee, getting the JSON object at each index. numEmployees.i = CkJsonArray::ckSize(employees) i.i = 0 While i < numEmployees empObj.i = CkJsonArray::ckObjectAt(employees,i) Debug "employee[" + Str(i) + "] = " + CkJsonObject::ckStringOf(empObj,"firstName") + " " + CkJsonObject::ckStringOf(empObj,"lastName") CkJsonObject::ckDispose(empObj) i = i + 1 Wend CkJsonArray::ckDispose(employees) CkJsonObject::ckDispose(json) ProcedureReturn EndProcedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.