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
(Xojo Plugin) 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.
// 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. Dim sb As New Chilkat.StringBuilder Dim bCrlf As Boolean bCrlf = True Dim success As Boolean success = sb.AppendLine("[",bCrlf) success = sb.AppendLine(" {",bCrlf) success = sb.AppendLine(" ""telefones"": [",bCrlf) success = sb.AppendLine(" {",bCrlf) success = sb.AppendLine(" ""numero"": ""19995555555"",",bCrlf) success = sb.AppendLine(" ""tipo"": ""T"",",bCrlf) success = sb.AppendLine(" ""id"": 2541437",bCrlf) success = sb.AppendLine(" }",bCrlf) success = sb.AppendLine(" ],",bCrlf) success = sb.AppendLine(" ""cnpj"": ""11395551000164"",",bCrlf) success = sb.AppendLine(" ""rua"": ""R XAVIER AUGUSTO ROGGE, 22"",",bCrlf) success = sb.AppendLine(" ""complemento"": """",",bCrlf) success = sb.AppendLine(" ""contatos"": [",bCrlf) success = sb.AppendLine(" ],",bCrlf) success = sb.AppendLine(" ""tipo"": ""J"",",bCrlf) success = sb.AppendLine(" ""razao_social"": ""SOUP BRASIL LTDA - ME"",",bCrlf) success = sb.AppendLine(" ""nome_fantasia"": ""SOUP BRASIL"",",bCrlf) success = sb.AppendLine(" ""bairro"": ""ABC DOS COLIBRIS"",",bCrlf) success = sb.AppendLine(" ""cidade"": ""TEST"",",bCrlf) success = sb.AppendLine(" ""inscricao_estadual"": ""222.102.222.116"",",bCrlf) success = sb.AppendLine(" ""observacao"": """",",bCrlf) success = sb.AppendLine(" ""id"": 2209595,",bCrlf) success = sb.AppendLine(" ""ultima_alteracao"": ""2016-12-26 16:22:34"",",bCrlf) success = sb.AppendLine(" ""cep"": ""13555000"",",bCrlf) success = sb.AppendLine(" ""suframa"": """",",bCrlf) success = sb.AppendLine(" ""estado"": ""SP"",",bCrlf) success = sb.AppendLine(" ""emails"": [",bCrlf) success = sb.AppendLine(" {",bCrlf) success = sb.AppendLine(" ""email"": ""somebody@terra.com.br"",",bCrlf) success = sb.AppendLine(" ""tipo"": ""T"",",bCrlf) success = sb.AppendLine(" ""id"": 1065557",bCrlf) success = sb.AppendLine(" }",bCrlf) success = sb.AppendLine(" ],",bCrlf) success = sb.AppendLine(" ""excluido"": false",bCrlf) success = sb.AppendLine(" },",bCrlf) success = sb.AppendLine(" {",bCrlf) success = sb.AppendLine(" ""telefones"": [",bCrlf) success = sb.AppendLine(" ],",bCrlf) success = sb.AppendLine(" ""cnpj"": ""12496555500180"",",bCrlf) success = sb.AppendLine(" ""rua"": ""AV ROLF WIEST, 100"",",bCrlf) success = sb.AppendLine(" ""complemento"": ""ANDAR 7 SALA 612 A 620"",",bCrlf) success = sb.AppendLine(" ""contatos"": [",bCrlf) success = sb.AppendLine(" ],",bCrlf) success = sb.AppendLine(" ""tipo"": ""J"",",bCrlf) success = sb.AppendLine(" ""razao_social"": ""SIMPLE SOFTWARE LTDA"",",bCrlf) success = sb.AppendLine(" ""nome_fantasia"": """",",bCrlf) success = sb.AppendLine(" ""bairro"": ""DOM ZETIRO"",",bCrlf) success = sb.AppendLine(" ""cidade"": ""APARTVILLE"",",bCrlf) success = sb.AppendLine(" ""inscricao_estadual"": """",",bCrlf) success = sb.AppendLine(" ""observacao"": """",",bCrlf) success = sb.AppendLine(" ""id"": 2255594,",bCrlf) success = sb.AppendLine(" ""ultima_alteracao"": ""2016-12-26 16:28:31"",",bCrlf) success = sb.AppendLine(" ""cep"": ""89255505"",",bCrlf) success = sb.AppendLine(" ""suframa"": """",",bCrlf) success = sb.AppendLine(" ""estado"": ""SC"",",bCrlf) success = sb.AppendLine(" ""emails"": [",bCrlf) success = sb.AppendLine(" ],",bCrlf) success = sb.AppendLine(" ""excluido"": false",bCrlf) success = sb.AppendLine(" },",bCrlf) success = sb.AppendLine(" {",bCrlf) success = sb.AppendLine(" ""telefones"": [",bCrlf) success = sb.AppendLine(" {",bCrlf) success = sb.AppendLine(" ""numero"": ""1938655556"",",bCrlf) success = sb.AppendLine(" ""tipo"": ""T"",",bCrlf) success = sb.AppendLine(" ""id"": 2555438",bCrlf) success = sb.AppendLine(" }",bCrlf) success = sb.AppendLine(" ],",bCrlf) success = sb.AppendLine(" ""cnpj"": ""00003555500153"",",bCrlf) success = sb.AppendLine(" ""rua"": ""AV ABCDEF PINTO CATAO, 18"",",bCrlf) success = sb.AppendLine(" ""complemento"": """",",bCrlf) success = sb.AppendLine(" ""contatos"": [",bCrlf) success = sb.AppendLine(" {",bCrlf) success = sb.AppendLine(" ""telefones"": [",bCrlf) success = sb.AppendLine(" {",bCrlf) success = sb.AppendLine(" ""numero"": ""1999655554"",",bCrlf) success = sb.AppendLine(" ""tipo"": ""T"",",bCrlf) success = sb.AppendLine(" ""id"": 2555559",bCrlf) success = sb.AppendLine(" }",bCrlf) success = sb.AppendLine(" ],",bCrlf) success = sb.AppendLine(" ""cargo"": ""zzz de compras"",",bCrlf) success = sb.AppendLine(" ""nome"": ""Gerard"",",bCrlf) success = sb.AppendLine(" ""emails"": [",bCrlf) success = sb.AppendLine(" {",bCrlf) success = sb.AppendLine(" ""email"": ""gerard@terra.com.br"",",bCrlf) success = sb.AppendLine(" ""tipo"": ""T"",",bCrlf) success = sb.AppendLine(" ""id"": 1065559",bCrlf) success = sb.AppendLine(" }",bCrlf) success = sb.AppendLine(" ],",bCrlf) success = sb.AppendLine(" ""id"": 844485,",bCrlf) success = sb.AppendLine(" ""excluido"": false",bCrlf) success = sb.AppendLine(" }",bCrlf) success = sb.AppendLine(" ],",bCrlf) success = sb.AppendLine(" ""tipo"": ""J"",",bCrlf) success = sb.AppendLine(" ""razao_social"": ""TIDY TECNOLOGIA LTDA - EPP"",",bCrlf) success = sb.AppendLine(" ""nome_fantasia"": ""TIDY"",",bCrlf) success = sb.AppendLine(" ""bairro"": ""TUNA"",",bCrlf) success = sb.AppendLine(" ""cidade"": ""JAGUAR"",",bCrlf) success = sb.AppendLine(" ""inscricao_estadual"": ""395.222.441.222"",",bCrlf) success = sb.AppendLine(" ""observacao"": ""ligar sempre depois das 14hs"",",bCrlf) success = sb.AppendLine(" ""id"": 2255597,",bCrlf) success = sb.AppendLine(" ""ultima_alteracao"": ""2016-12-28 07:31:52"",",bCrlf) success = sb.AppendLine(" ""cep"": ""13555500"",",bCrlf) success = sb.AppendLine(" ""suframa"": """",",bCrlf) success = sb.AppendLine(" ""estado"": ""SP"",",bCrlf) success = sb.AppendLine(" ""emails"": [",bCrlf) success = sb.AppendLine(" {",bCrlf) success = sb.AppendLine(" ""email"": ""xi@tidy.com.br"",",bCrlf) success = sb.AppendLine(" ""tipo"": ""T"",",bCrlf) success = sb.AppendLine(" ""id"": 10655558",bCrlf) success = sb.AppendLine(" }",bCrlf) success = sb.AppendLine(" ],",bCrlf) success = sb.AppendLine(" ""excluido"": false",bCrlf) success = sb.AppendLine(" }",bCrlf) success = sb.AppendLine("]",bCrlf) // Load the JSON array into a JsonArray: Dim jsonArray As New Chilkat.JsonArray Dim success As Boolean success = jsonArray.LoadSb(sb) If (success <> True) Then System.DebugLog(jsonArray.LastErrorText) Return End If // Get some information from each record in the array. Dim numRecords As Int32 numRecords = jsonArray.Size Dim i As Int32 i = 0 While i < numRecords System.DebugLog("------ Record " + Str(i) + " -------") Dim jsonRecord As Chilkat.JsonObject jsonRecord = jsonArray.ObjectAt(i) // Examine information for this record Dim numTelefones As Int32 numTelefones = jsonRecord.SizeOfArray("telefones") System.DebugLog("Number of telefones: " + Str(numTelefones)) Dim j As Int32 j = 0 While j < numTelefones jsonRecord.J = j System.DebugLog(" telefones numero: " + jsonRecord.StringOf("telefones[j].numero")) System.DebugLog(" telefones tipo: " + jsonRecord.StringOf("telefones[j].tipo")) System.DebugLog(" telefones id: " + jsonRecord.StringOf("telefones[j].id")) j = j + 1 Wend System.DebugLog("cnpj: " + jsonRecord.StringOf("cnpj")) System.DebugLog("rua: " + jsonRecord.StringOf("rua")) // ... Dim numContatos As Int32 numContatos = jsonRecord.SizeOfArray("contatos") System.DebugLog("Number of contatos: " + Str(numContatos)) j = 0 While j < numContatos jsonRecord.J = j numTelefones = jsonRecord.SizeOfArray("contatos[j].telefones") System.DebugLog(" Number of telefones: " + Str(numTelefones)) Dim k As Int32 k = 0 While k < numTelefones jsonRecord.K = k System.DebugLog(" telefones numero: " + jsonRecord.StringOf("contatos[j].telefones[k].numero")) System.DebugLog(" telefones tipo: " + jsonRecord.StringOf("contatos[j].telefones[k].tipo")) System.DebugLog(" telefones id: " + jsonRecord.StringOf("contatos[j].telefones[k].id")) k = k + 1 Wend System.DebugLog(" cargo: " + jsonRecord.StringOf("contatos[j].cargo")) Dim numEmails As Int32 numEmails = jsonRecord.SizeOfArray("contatos[j].emails") System.DebugLog(" Number of emails: " + Str(numEmails)) k = 0 While k < numEmails jsonRecord.K = k System.DebugLog(" emails email: " + jsonRecord.StringOf("contatos[j].emails[k].email")) System.DebugLog(" emails tipo: " + jsonRecord.StringOf("contatos[j].emails[k].tipo")) System.DebugLog(" emails id: " + jsonRecord.StringOf("contatos[j].emails[k].id")) k = k + 1 Wend j = j + 1 Wend 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 // |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.