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) 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.
IncludeFile "CkStringBuilder.pb" IncludeFile "CkJsonArray.pb" IncludeFile "CkJsonObject.pb" Procedure ChilkatExample() ; 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. sb.i = CkStringBuilder::ckCreate() If sb.i = 0 Debug "Failed to create object." ProcedureReturn EndIf bCrlf.i = 1 CkStringBuilder::ckAppendLine(sb,"[",bCrlf) CkStringBuilder::ckAppendLine(sb," {",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "telefones" + Chr(34) + ": [",bCrlf) CkStringBuilder::ckAppendLine(sb," {",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "numero" + Chr(34) + ": " + Chr(34) + "19995555555" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "tipo" + Chr(34) + ": " + Chr(34) + "T" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "id" + Chr(34) + ": 2541437",bCrlf) CkStringBuilder::ckAppendLine(sb," }",bCrlf) CkStringBuilder::ckAppendLine(sb," ],",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "cnpj" + Chr(34) + ": " + Chr(34) + "11395551000164" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "rua" + Chr(34) + ": " + Chr(34) + "R XAVIER AUGUSTO ROGGE, 22" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "complemento" + Chr(34) + ": " + Chr(34) + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "contatos" + Chr(34) + ": [",bCrlf) CkStringBuilder::ckAppendLine(sb," ],",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "tipo" + Chr(34) + ": " + Chr(34) + "J" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "razao_social" + Chr(34) + ": " + Chr(34) + "SOUP BRASIL LTDA - ME" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "nome_fantasia" + Chr(34) + ": " + Chr(34) + "SOUP BRASIL" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "bairro" + Chr(34) + ": " + Chr(34) + "ABC DOS COLIBRIS" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "cidade" + Chr(34) + ": " + Chr(34) + "TEST" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "inscricao_estadual" + Chr(34) + ": " + Chr(34) + "222.102.222.116" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "observacao" + Chr(34) + ": " + Chr(34) + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "id" + Chr(34) + ": 2209595,",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "ultima_alteracao" + Chr(34) + ": " + Chr(34) + "2016-12-26 16:22:34" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "cep" + Chr(34) + ": " + Chr(34) + "13555000" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "suframa" + Chr(34) + ": " + Chr(34) + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "estado" + Chr(34) + ": " + Chr(34) + "SP" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "emails" + Chr(34) + ": [",bCrlf) CkStringBuilder::ckAppendLine(sb," {",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "email" + Chr(34) + ": " + Chr(34) + "somebody@terra.com.br" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "tipo" + Chr(34) + ": " + Chr(34) + "T" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "id" + Chr(34) + ": 1065557",bCrlf) CkStringBuilder::ckAppendLine(sb," }",bCrlf) CkStringBuilder::ckAppendLine(sb," ],",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "excluido" + Chr(34) + ": false",bCrlf) CkStringBuilder::ckAppendLine(sb," },",bCrlf) CkStringBuilder::ckAppendLine(sb," {",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "telefones" + Chr(34) + ": [",bCrlf) CkStringBuilder::ckAppendLine(sb," ],",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "cnpj" + Chr(34) + ": " + Chr(34) + "12496555500180" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "rua" + Chr(34) + ": " + Chr(34) + "AV ROLF WIEST, 100" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "complemento" + Chr(34) + ": " + Chr(34) + "ANDAR 7 SALA 612 A 620" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "contatos" + Chr(34) + ": [",bCrlf) CkStringBuilder::ckAppendLine(sb," ],",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "tipo" + Chr(34) + ": " + Chr(34) + "J" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "razao_social" + Chr(34) + ": " + Chr(34) + "SIMPLE SOFTWARE LTDA" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "nome_fantasia" + Chr(34) + ": " + Chr(34) + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "bairro" + Chr(34) + ": " + Chr(34) + "DOM ZETIRO" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "cidade" + Chr(34) + ": " + Chr(34) + "APARTVILLE" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "inscricao_estadual" + Chr(34) + ": " + Chr(34) + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "observacao" + Chr(34) + ": " + Chr(34) + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "id" + Chr(34) + ": 2255594,",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "ultima_alteracao" + Chr(34) + ": " + Chr(34) + "2016-12-26 16:28:31" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "cep" + Chr(34) + ": " + Chr(34) + "89255505" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "suframa" + Chr(34) + ": " + Chr(34) + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "estado" + Chr(34) + ": " + Chr(34) + "SC" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "emails" + Chr(34) + ": [",bCrlf) CkStringBuilder::ckAppendLine(sb," ],",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "excluido" + Chr(34) + ": false",bCrlf) CkStringBuilder::ckAppendLine(sb," },",bCrlf) CkStringBuilder::ckAppendLine(sb," {",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "telefones" + Chr(34) + ": [",bCrlf) CkStringBuilder::ckAppendLine(sb," {",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "numero" + Chr(34) + ": " + Chr(34) + "1938655556" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "tipo" + Chr(34) + ": " + Chr(34) + "T" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "id" + Chr(34) + ": 2555438",bCrlf) CkStringBuilder::ckAppendLine(sb," }",bCrlf) CkStringBuilder::ckAppendLine(sb," ],",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "cnpj" + Chr(34) + ": " + Chr(34) + "00003555500153" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "rua" + Chr(34) + ": " + Chr(34) + "AV ABCDEF PINTO CATAO, 18" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "complemento" + Chr(34) + ": " + Chr(34) + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "contatos" + Chr(34) + ": [",bCrlf) CkStringBuilder::ckAppendLine(sb," {",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "telefones" + Chr(34) + ": [",bCrlf) CkStringBuilder::ckAppendLine(sb," {",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "numero" + Chr(34) + ": " + Chr(34) + "1999655554" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "tipo" + Chr(34) + ": " + Chr(34) + "T" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "id" + Chr(34) + ": 2555559",bCrlf) CkStringBuilder::ckAppendLine(sb," }",bCrlf) CkStringBuilder::ckAppendLine(sb," ],",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "cargo" + Chr(34) + ": " + Chr(34) + "zzz de compras" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "nome" + Chr(34) + ": " + Chr(34) + "Gerard" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "emails" + Chr(34) + ": [",bCrlf) CkStringBuilder::ckAppendLine(sb," {",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "email" + Chr(34) + ": " + Chr(34) + "gerard@terra.com.br" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "tipo" + Chr(34) + ": " + Chr(34) + "T" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "id" + Chr(34) + ": 1065559",bCrlf) CkStringBuilder::ckAppendLine(sb," }",bCrlf) CkStringBuilder::ckAppendLine(sb," ],",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "id" + Chr(34) + ": 844485,",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "excluido" + Chr(34) + ": false",bCrlf) CkStringBuilder::ckAppendLine(sb," }",bCrlf) CkStringBuilder::ckAppendLine(sb," ],",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "tipo" + Chr(34) + ": " + Chr(34) + "J" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "razao_social" + Chr(34) + ": " + Chr(34) + "TIDY TECNOLOGIA LTDA - EPP" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "nome_fantasia" + Chr(34) + ": " + Chr(34) + "TIDY" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "bairro" + Chr(34) + ": " + Chr(34) + "TUNA" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "cidade" + Chr(34) + ": " + Chr(34) + "JAGUAR" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "inscricao_estadual" + Chr(34) + ": " + Chr(34) + "395.222.441.222" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "observacao" + Chr(34) + ": " + Chr(34) + "ligar sempre depois das 14hs" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "id" + Chr(34) + ": 2255597,",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "ultima_alteracao" + Chr(34) + ": " + Chr(34) + "2016-12-28 07:31:52" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "cep" + Chr(34) + ": " + Chr(34) + "13555500" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "suframa" + Chr(34) + ": " + Chr(34) + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "estado" + Chr(34) + ": " + Chr(34) + "SP" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "emails" + Chr(34) + ": [",bCrlf) CkStringBuilder::ckAppendLine(sb," {",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "email" + Chr(34) + ": " + Chr(34) + "xi@tidy.com.br" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "tipo" + Chr(34) + ": " + Chr(34) + "T" + Chr(34) + ",",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "id" + Chr(34) + ": 10655558",bCrlf) CkStringBuilder::ckAppendLine(sb," }",bCrlf) CkStringBuilder::ckAppendLine(sb," ],",bCrlf) CkStringBuilder::ckAppendLine(sb," " + Chr(34) + "excluido" + Chr(34) + ": false",bCrlf) CkStringBuilder::ckAppendLine(sb," }",bCrlf) CkStringBuilder::ckAppendLine(sb,"]",bCrlf) ; Load the JSON array into a JsonArray: jsonArray.i = CkJsonArray::ckCreate() If jsonArray.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success.i = CkJsonArray::ckLoadSb(jsonArray,sb) If success <> 1 Debug CkJsonArray::ckLastErrorText(jsonArray) CkStringBuilder::ckDispose(sb) CkJsonArray::ckDispose(jsonArray) ProcedureReturn EndIf ; Get some information from each record in the array. numRecords.i = CkJsonArray::ckSize(jsonArray) i.i = 0 While i < numRecords Debug "------ Record " + Str(i) + " -------" jsonRecord.i = CkJsonArray::ckObjectAt(jsonArray,i) ; Examine information for this record numTelefones.i = CkJsonObject::ckSizeOfArray(jsonRecord,"telefones") Debug "Number of telefones: " + Str(numTelefones) j.i = 0 While j < numTelefones CkJsonObject::setCkJ(jsonRecord, j) Debug " telefones numero: " + CkJsonObject::ckStringOf(jsonRecord,"telefones[j].numero") Debug " telefones tipo: " + CkJsonObject::ckStringOf(jsonRecord,"telefones[j].tipo") Debug " telefones id: " + CkJsonObject::ckStringOf(jsonRecord,"telefones[j].id") j = j + 1 Wend Debug "cnpj: " + CkJsonObject::ckStringOf(jsonRecord,"cnpj") Debug "rua: " + CkJsonObject::ckStringOf(jsonRecord,"rua") ; ... numContatos.i = CkJsonObject::ckSizeOfArray(jsonRecord,"contatos") Debug "Number of contatos: " + Str(numContatos) j = 0 While j < numContatos CkJsonObject::setCkJ(jsonRecord, j) numTelefones = CkJsonObject::ckSizeOfArray(jsonRecord,"contatos[j].telefones") Debug " Number of telefones: " + Str(numTelefones) k.i = 0 While k < numTelefones CkJsonObject::setCkK(jsonRecord, k) Debug " telefones numero: " + CkJsonObject::ckStringOf(jsonRecord,"contatos[j].telefones[k].numero") Debug " telefones tipo: " + CkJsonObject::ckStringOf(jsonRecord,"contatos[j].telefones[k].tipo") Debug " telefones id: " + CkJsonObject::ckStringOf(jsonRecord,"contatos[j].telefones[k].id") k = k + 1 Wend Debug " cargo: " + CkJsonObject::ckStringOf(jsonRecord,"contatos[j].cargo") numEmails.i = CkJsonObject::ckSizeOfArray(jsonRecord,"contatos[j].emails") Debug " Number of emails: " + Str(numEmails) k = 0 While k < numEmails CkJsonObject::setCkK(jsonRecord, k) Debug " emails email: " + CkJsonObject::ckStringOf(jsonRecord,"contatos[j].emails[k].email") Debug " emails tipo: " + CkJsonObject::ckStringOf(jsonRecord,"contatos[j].emails[k].tipo") Debug " emails id: " + CkJsonObject::ckStringOf(jsonRecord,"contatos[j].emails[k].id") k = k + 1 Wend j = j + 1 Wend CkJsonObject::ckDispose(jsonRecord) i = i + 1 Wend ; 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 ; CkStringBuilder::ckDispose(sb) CkJsonArray::ckDispose(jsonArray) ProcedureReturn EndProcedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.