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
(DataFlex) Loading and Parsing a Complex JSON ArrayThis example loads a JSON array containing more complex data. It shows how to parse (access) various values contained within the JSON.
Use ChilkatAx-win32.pkg Procedure Test Variant vSb Handle hoSb Boolean iBCrlf Boolean iSuccess Handle hoJsonArray Boolean iSuccess Integer iNumRecords Integer i Variant vJsonRecord Handle hoJsonRecord Integer iNumTelefones Integer j Integer iNumContatos Integer k Integer iNumEmails String sTemp1 // This is the JSON we'll be loading: // [ // { // "telefones": [ // { // "numero": "19995555555", // "tipo": "T", // "id": 2541437 // } // ], // "cnpj": "11395551000164", // "rua": "R XAVIER AUGUSTO ROGGE, 22", // "complemento": "", // "contatos": [ // ], // "tipo": "J", // "razao_social": "SOUP BRASIL LTDA - ME", // "nome_fantasia": "SOUP BRASIL", // "bairro": "ABC DOS COLIBRIS", // "cidade": "TEST", // "inscricao_estadual": "222.102.222.116", // "observacao": "", // "id": 2209595, // "ultima_alteracao": "2016-12-26 16:22:34", // "cep": "13555000", // "suframa": "", // "estado": "SP", // "emails": [ // { // "email": "somebody@terra.com.br", // "tipo": "T", // "id": 1065557 // } // ], // "excluido": false // }, // { // "telefones": [ // ], // "cnpj": "12496555500180", // "rua": "AV ROLF WIEST, 100", // "complemento": "ANDAR 7 SALA 612 A 620", // "contatos": [ // ], // "tipo": "J", // "razao_social": "SIMPLE SOFTWARE LTDA", // "nome_fantasia": "", // "bairro": "DOM ZETIRO", // "cidade": "APARTVILLE", // "inscricao_estadual": "", // "observacao": "", // "id": 2255594, // "ultima_alteracao": "2016-12-26 16:28:31", // "cep": "89255505", // "suframa": "", // "estado": "SC", // "emails": [ // ], // "excluido": false // }, // { // "telefones": [ // { // "numero": "1938655556", // "tipo": "T", // "id": 2555438 // } // ], // "cnpj": "00003555500153", // "rua": "AV ABCDEF PINTO CATAO, 18", // "complemento": "", // "contatos": [ // { // "telefones": [ // { // "numero": "1999655554", // "tipo": "T", // "id": 2555559 // } // ], // "cargo": "zzz de compras", // "nome": "Gerard", // "emails": [ // { // "email": "gerard@terra.com.br", // "tipo": "T", // "id": 1065559 // } // ], // "id": 844485, // "excluido": false // } // ], // "tipo": "J", // "razao_social": "TIDY TECNOLOGIA LTDA - EPP", // "nome_fantasia": "TIDY", // "bairro": "TUNA", // "cidade": "JAGUAR", // "inscricao_estadual": "395.222.441.222", // "observacao": "ligar sempre depois das 14hs", // "id": 2255597, // "ultima_alteracao": "2016-12-28 07:31:52", // "cep": "13555500", // "suframa": "", // "estado": "SP", // "emails": [ // { // "email": "xi@tidy.com.br", // "tipo": "T", // "id": 10655558 // } // ], // "excluido": false // } // ] // // Construct a StringBuilder containing the above JSON array. Get Create (RefClass(cComChilkatStringBuilder)) To hoSb If (Not(IsComObjectCreated(hoSb))) Begin Send CreateComObject of hoSb End Move True To iBCrlf Get ComAppendLine Of hoSb "[" iBCrlf To iSuccess Get ComAppendLine Of hoSb " {" iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "telefones": [' iBCrlf To iSuccess Get ComAppendLine Of hoSb " {" iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "numero": "19995555555",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "tipo": "T",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "id": 2541437' iBCrlf To iSuccess Get ComAppendLine Of hoSb " }" iBCrlf To iSuccess Get ComAppendLine Of hoSb " ]," iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "cnpj": "11395551000164",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "rua": "R XAVIER AUGUSTO ROGGE, 22",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "complemento": "",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "contatos": [' iBCrlf To iSuccess Get ComAppendLine Of hoSb " ]," iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "tipo": "J",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "razao_social": "SOUP BRASIL LTDA - ME",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "nome_fantasia": "SOUP BRASIL",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "bairro": "ABC DOS COLIBRIS",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "cidade": "TEST",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "inscricao_estadual": "222.102.222.116",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "observacao": "",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "id": 2209595,' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "ultima_alteracao": "2016-12-26 16:22:34",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "cep": "13555000",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "suframa": "",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "estado": "SP",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "emails": [' iBCrlf To iSuccess Get ComAppendLine Of hoSb " {" iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "email": "somebody@terra.com.br",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "tipo": "T",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "id": 1065557' iBCrlf To iSuccess Get ComAppendLine Of hoSb " }" iBCrlf To iSuccess Get ComAppendLine Of hoSb " ]," iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "excluido": false' iBCrlf To iSuccess Get ComAppendLine Of hoSb " }," iBCrlf To iSuccess Get ComAppendLine Of hoSb " {" iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "telefones": [' iBCrlf To iSuccess Get ComAppendLine Of hoSb " ]," iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "cnpj": "12496555500180",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "rua": "AV ROLF WIEST, 100",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "complemento": "ANDAR 7 SALA 612 A 620",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "contatos": [' iBCrlf To iSuccess Get ComAppendLine Of hoSb " ]," iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "tipo": "J",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "razao_social": "SIMPLE SOFTWARE LTDA",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "nome_fantasia": "",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "bairro": "DOM ZETIRO",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "cidade": "APARTVILLE",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "inscricao_estadual": "",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "observacao": "",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "id": 2255594,' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "ultima_alteracao": "2016-12-26 16:28:31",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "cep": "89255505",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "suframa": "",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "estado": "SC",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "emails": [' iBCrlf To iSuccess Get ComAppendLine Of hoSb " ]," iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "excluido": false' iBCrlf To iSuccess Get ComAppendLine Of hoSb " }," iBCrlf To iSuccess Get ComAppendLine Of hoSb " {" iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "telefones": [' iBCrlf To iSuccess Get ComAppendLine Of hoSb " {" iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "numero": "1938655556",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "tipo": "T",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "id": 2555438' iBCrlf To iSuccess Get ComAppendLine Of hoSb " }" iBCrlf To iSuccess Get ComAppendLine Of hoSb " ]," iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "cnpj": "00003555500153",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "rua": "AV ABCDEF PINTO CATAO, 18",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "complemento": "",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "contatos": [' iBCrlf To iSuccess Get ComAppendLine Of hoSb " {" iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "telefones": [' iBCrlf To iSuccess Get ComAppendLine Of hoSb " {" iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "numero": "1999655554",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "tipo": "T",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "id": 2555559' iBCrlf To iSuccess Get ComAppendLine Of hoSb " }" iBCrlf To iSuccess Get ComAppendLine Of hoSb " ]," iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "cargo": "zzz de compras",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "nome": "Gerard",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "emails": [' iBCrlf To iSuccess Get ComAppendLine Of hoSb " {" iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "email": "gerard@terra.com.br",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "tipo": "T",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "id": 1065559' iBCrlf To iSuccess Get ComAppendLine Of hoSb " }" iBCrlf To iSuccess Get ComAppendLine Of hoSb " ]," iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "id": 844485,' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "excluido": false' iBCrlf To iSuccess Get ComAppendLine Of hoSb " }" iBCrlf To iSuccess Get ComAppendLine Of hoSb " ]," iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "tipo": "J",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "razao_social": "TIDY TECNOLOGIA LTDA - EPP",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "nome_fantasia": "TIDY",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "bairro": "TUNA",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "cidade": "JAGUAR",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "inscricao_estadual": "395.222.441.222",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "observacao": "ligar sempre depois das 14hs",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "id": 2255597,' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "ultima_alteracao": "2016-12-28 07:31:52",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "cep": "13555500",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "suframa": "",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "estado": "SP",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "emails": [' iBCrlf To iSuccess Get ComAppendLine Of hoSb " {" iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "email": "xi@tidy.com.br",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "tipo": "T",' iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "id": 10655558' iBCrlf To iSuccess Get ComAppendLine Of hoSb " }" iBCrlf To iSuccess Get ComAppendLine Of hoSb " ]," iBCrlf To iSuccess Get ComAppendLine Of hoSb ' "excluido": false' iBCrlf To iSuccess Get ComAppendLine Of hoSb " }" iBCrlf To iSuccess Get ComAppendLine Of hoSb "]" iBCrlf To iSuccess // Load the JSON array into a JsonArray: Get Create (RefClass(cComChilkatJsonArray)) To hoJsonArray If (Not(IsComObjectCreated(hoJsonArray))) Begin Send CreateComObject of hoJsonArray End Get pvComObject of hoSb to vSb Get ComLoadSb Of hoJsonArray vSb To iSuccess If (iSuccess <> True) Begin Get ComLastErrorText Of hoJsonArray To sTemp1 Showln sTemp1 Procedure_Return End // Get some information from each record in the array. Get ComSize Of hoJsonArray To iNumRecords Move 0 To i While (i < iNumRecords) Showln "------ Record " i " -------" Get ComObjectAt Of hoJsonArray i To vJsonRecord If (IsComObject(vJsonRecord)) Begin Get Create (RefClass(cComChilkatJsonObject)) To hoJsonRecord Set pvComObject Of hoJsonRecord To vJsonRecord End // Examine information for this record Get ComSizeOfArray Of hoJsonRecord "telefones" To iNumTelefones Showln "Number of telefones: " iNumTelefones Move 0 To j While (j < iNumTelefones) Set ComJ Of hoJsonRecord To j Get ComStringOf Of hoJsonRecord "telefones[j].numero" To sTemp1 Showln " telefones numero: " sTemp1 Get ComStringOf Of hoJsonRecord "telefones[j].tipo" To sTemp1 Showln " telefones tipo: " sTemp1 Get ComStringOf Of hoJsonRecord "telefones[j].id" To sTemp1 Showln " telefones id: " sTemp1 Move (j + 1) To j Loop Get ComStringOf Of hoJsonRecord "cnpj" To sTemp1 Showln "cnpj: " sTemp1 Get ComStringOf Of hoJsonRecord "rua" To sTemp1 Showln "rua: " sTemp1 // ... Get ComSizeOfArray Of hoJsonRecord "contatos" To iNumContatos Showln "Number of contatos: " iNumContatos Move 0 To j While (j < iNumContatos) Set ComJ Of hoJsonRecord To j Get ComSizeOfArray Of hoJsonRecord "contatos[j].telefones" To iNumTelefones Showln " Number of telefones: " iNumTelefones Move 0 To k While (k < iNumTelefones) Set ComK Of hoJsonRecord To k Get ComStringOf Of hoJsonRecord "contatos[j].telefones[k].numero" To sTemp1 Showln " telefones numero: " sTemp1 Get ComStringOf Of hoJsonRecord "contatos[j].telefones[k].tipo" To sTemp1 Showln " telefones tipo: " sTemp1 Get ComStringOf Of hoJsonRecord "contatos[j].telefones[k].id" To sTemp1 Showln " telefones id: " sTemp1 Move (k + 1) To k Loop Get ComStringOf Of hoJsonRecord "contatos[j].cargo" To sTemp1 Showln " cargo: " sTemp1 Get ComSizeOfArray Of hoJsonRecord "contatos[j].emails" To iNumEmails Showln " Number of emails: " iNumEmails Move 0 To k While (k < iNumEmails) Set ComK Of hoJsonRecord To k Get ComStringOf Of hoJsonRecord "contatos[j].emails[k].email" To sTemp1 Showln " emails email: " sTemp1 Get ComStringOf Of hoJsonRecord "contatos[j].emails[k].tipo" To sTemp1 Showln " emails tipo: " sTemp1 Get ComStringOf Of hoJsonRecord "contatos[j].emails[k].id" To sTemp1 Showln " emails id: " sTemp1 Move (k + 1) To k Loop Move (j + 1) To j Loop Send Destroy of hoJsonRecord Move (i + 1) To i Loop // The output for the above code is: // ------ Record 0 ------- // Number of telefones: 1 // telefones numero: 19995555555 // telefones tipo: T // telefones id: 2541437 // cnpj: 11395551000164 // rua: R XAVIER AUGUSTO ROGGE, 22 // Number of contatos: 0 // ------ Record 1 ------- // Number of telefones: 0 // cnpj: 12496555500180 // rua: AV ROLF WIEST, 100 // Number of contatos: 0 // ------ Record 2 ------- // Number of telefones: 1 // telefones numero: 1938655556 // telefones tipo: T // telefones id: 2555438 // cnpj: 00003555500153 // rua: AV ABCDEF PINTO CATAO, 18 // Number of contatos: 1 // Number of telefones: 1 // telefones numero: 1999655554 // telefones tipo: T // telefones id: 2555559 // cargo: zzz de compras // Number of emails: 1 // emails email: gerard@terra.com.br // emails tipo: T // emails id: 1065559 // End_Procedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.