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
(Unicode C) 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.
#include <C_CkStringBuilderW.h> #include <C_CkJsonArrayW.h> #include <C_CkJsonObjectW.h> void ChilkatSample(void) { HCkStringBuilderW sb; BOOL bCrlf; HCkJsonArrayW jsonArray; BOOL success; int numRecords; int i; HCkJsonObjectW jsonRecord; int numTelefones; int j; int numContatos; int k; int numEmails; // 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 = CkStringBuilderW_Create(); bCrlf = TRUE; CkStringBuilderW_AppendLine(sb,L"[",bCrlf); CkStringBuilderW_AppendLine(sb,L" {",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"telefones\": [",bCrlf); CkStringBuilderW_AppendLine(sb,L" {",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"numero\": \"19995555555\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"tipo\": \"T\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"id\": 2541437",bCrlf); CkStringBuilderW_AppendLine(sb,L" }",bCrlf); CkStringBuilderW_AppendLine(sb,L" ],",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"cnpj\": \"11395551000164\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"rua\": \"R XAVIER AUGUSTO ROGGE, 22\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"complemento\": \"\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"contatos\": [",bCrlf); CkStringBuilderW_AppendLine(sb,L" ],",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"tipo\": \"J\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"razao_social\": \"SOUP BRASIL LTDA - ME\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"nome_fantasia\": \"SOUP BRASIL\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"bairro\": \"ABC DOS COLIBRIS\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"cidade\": \"TEST\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"inscricao_estadual\": \"222.102.222.116\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"observacao\": \"\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"id\": 2209595,",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"ultima_alteracao\": \"2016-12-26 16:22:34\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"cep\": \"13555000\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"suframa\": \"\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"estado\": \"SP\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"emails\": [",bCrlf); CkStringBuilderW_AppendLine(sb,L" {",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"email\": \"somebody@terra.com.br\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"tipo\": \"T\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"id\": 1065557",bCrlf); CkStringBuilderW_AppendLine(sb,L" }",bCrlf); CkStringBuilderW_AppendLine(sb,L" ],",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"excluido\": false",bCrlf); CkStringBuilderW_AppendLine(sb,L" },",bCrlf); CkStringBuilderW_AppendLine(sb,L" {",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"telefones\": [",bCrlf); CkStringBuilderW_AppendLine(sb,L" ],",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"cnpj\": \"12496555500180\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"rua\": \"AV ROLF WIEST, 100\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"complemento\": \"ANDAR 7 SALA 612 A 620\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"contatos\": [",bCrlf); CkStringBuilderW_AppendLine(sb,L" ],",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"tipo\": \"J\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"razao_social\": \"SIMPLE SOFTWARE LTDA\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"nome_fantasia\": \"\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"bairro\": \"DOM ZETIRO\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"cidade\": \"APARTVILLE\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"inscricao_estadual\": \"\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"observacao\": \"\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"id\": 2255594,",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"ultima_alteracao\": \"2016-12-26 16:28:31\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"cep\": \"89255505\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"suframa\": \"\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"estado\": \"SC\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"emails\": [",bCrlf); CkStringBuilderW_AppendLine(sb,L" ],",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"excluido\": false",bCrlf); CkStringBuilderW_AppendLine(sb,L" },",bCrlf); CkStringBuilderW_AppendLine(sb,L" {",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"telefones\": [",bCrlf); CkStringBuilderW_AppendLine(sb,L" {",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"numero\": \"1938655556\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"tipo\": \"T\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"id\": 2555438",bCrlf); CkStringBuilderW_AppendLine(sb,L" }",bCrlf); CkStringBuilderW_AppendLine(sb,L" ],",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"cnpj\": \"00003555500153\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"rua\": \"AV ABCDEF PINTO CATAO, 18\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"complemento\": \"\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"contatos\": [",bCrlf); CkStringBuilderW_AppendLine(sb,L" {",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"telefones\": [",bCrlf); CkStringBuilderW_AppendLine(sb,L" {",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"numero\": \"1999655554\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"tipo\": \"T\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"id\": 2555559",bCrlf); CkStringBuilderW_AppendLine(sb,L" }",bCrlf); CkStringBuilderW_AppendLine(sb,L" ],",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"cargo\": \"zzz de compras\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"nome\": \"Gerard\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"emails\": [",bCrlf); CkStringBuilderW_AppendLine(sb,L" {",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"email\": \"gerard@terra.com.br\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"tipo\": \"T\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"id\": 1065559",bCrlf); CkStringBuilderW_AppendLine(sb,L" }",bCrlf); CkStringBuilderW_AppendLine(sb,L" ],",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"id\": 844485,",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"excluido\": false",bCrlf); CkStringBuilderW_AppendLine(sb,L" }",bCrlf); CkStringBuilderW_AppendLine(sb,L" ],",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"tipo\": \"J\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"razao_social\": \"TIDY TECNOLOGIA LTDA - EPP\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"nome_fantasia\": \"TIDY\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"bairro\": \"TUNA\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"cidade\": \"JAGUAR\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"inscricao_estadual\": \"395.222.441.222\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"observacao\": \"ligar sempre depois das 14hs\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"id\": 2255597,",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"ultima_alteracao\": \"2016-12-28 07:31:52\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"cep\": \"13555500\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"suframa\": \"\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"estado\": \"SP\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"emails\": [",bCrlf); CkStringBuilderW_AppendLine(sb,L" {",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"email\": \"xi@tidy.com.br\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"tipo\": \"T\",",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"id\": 10655558",bCrlf); CkStringBuilderW_AppendLine(sb,L" }",bCrlf); CkStringBuilderW_AppendLine(sb,L" ],",bCrlf); CkStringBuilderW_AppendLine(sb,L" \"excluido\": false",bCrlf); CkStringBuilderW_AppendLine(sb,L" }",bCrlf); CkStringBuilderW_AppendLine(sb,L"]",bCrlf); // Load the JSON array into a JsonArray: jsonArray = CkJsonArrayW_Create(); success = CkJsonArrayW_LoadSb(jsonArray,sb); if (success != TRUE) { wprintf(L"%s\n",CkJsonArrayW_lastErrorText(jsonArray)); CkStringBuilderW_Dispose(sb); CkJsonArrayW_Dispose(jsonArray); return; } // Get some information from each record in the array. numRecords = CkJsonArrayW_getSize(jsonArray); i = 0; while (i < numRecords) { wprintf(L"------ Record %d -------\n",i); jsonRecord = CkJsonArrayW_ObjectAt(jsonArray,i); // Examine information for this record numTelefones = CkJsonObjectW_SizeOfArray(jsonRecord,L"telefones"); wprintf(L"Number of telefones: %d\n",numTelefones); j = 0; while (j < numTelefones) { CkJsonObjectW_putJ(jsonRecord,j); wprintf(L" telefones numero: %s\n",CkJsonObjectW_stringOf(jsonRecord,L"telefones[j].numero")); wprintf(L" telefones tipo: %s\n",CkJsonObjectW_stringOf(jsonRecord,L"telefones[j].tipo")); wprintf(L" telefones id: %s\n",CkJsonObjectW_stringOf(jsonRecord,L"telefones[j].id")); j = j + 1; } wprintf(L"cnpj: %s\n",CkJsonObjectW_stringOf(jsonRecord,L"cnpj")); wprintf(L"rua: %s\n",CkJsonObjectW_stringOf(jsonRecord,L"rua")); // ... numContatos = CkJsonObjectW_SizeOfArray(jsonRecord,L"contatos"); wprintf(L"Number of contatos: %d\n",numContatos); j = 0; while (j < numContatos) { CkJsonObjectW_putJ(jsonRecord,j); numTelefones = CkJsonObjectW_SizeOfArray(jsonRecord,L"contatos[j].telefones"); wprintf(L" Number of telefones: %d\n",numTelefones); k = 0; while (k < numTelefones) { CkJsonObjectW_putK(jsonRecord,k); wprintf(L" telefones numero: %s\n",CkJsonObjectW_stringOf(jsonRecord,L"contatos[j].telefones[k].numero")); wprintf(L" telefones tipo: %s\n",CkJsonObjectW_stringOf(jsonRecord,L"contatos[j].telefones[k].tipo")); wprintf(L" telefones id: %s\n",CkJsonObjectW_stringOf(jsonRecord,L"contatos[j].telefones[k].id")); k = k + 1; } wprintf(L" cargo: %s\n",CkJsonObjectW_stringOf(jsonRecord,L"contatos[j].cargo")); numEmails = CkJsonObjectW_SizeOfArray(jsonRecord,L"contatos[j].emails"); wprintf(L" Number of emails: %d\n",numEmails); k = 0; while (k < numEmails) { CkJsonObjectW_putK(jsonRecord,k); wprintf(L" emails email: %s\n",CkJsonObjectW_stringOf(jsonRecord,L"contatos[j].emails[k].email")); wprintf(L" emails tipo: %s\n",CkJsonObjectW_stringOf(jsonRecord,L"contatos[j].emails[k].tipo")); wprintf(L" emails id: %s\n",CkJsonObjectW_stringOf(jsonRecord,L"contatos[j].emails[k].id")); k = k + 1; } j = j + 1; } CkJsonObjectW_Dispose(jsonRecord); i = i + 1; } // 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 // CkStringBuilderW_Dispose(sb); CkJsonArrayW_Dispose(jsonArray); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.