Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

Objective-C Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(Objective-C) Loading and Parsing a Complex JSON Array

This example loads a JSON array containing more complex data. It shows how to parse (access) various values contained within the JSON.

Chilkat Objective-C Library Downloads

MAC OS X (Cocoa) Libs

iOS Libs

#import <CkoStringBuilder.h>
#import <CkoJsonArray.h>
#import <CkoJsonObject.h>

// 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.
CkoStringBuilder *sb = [[CkoStringBuilder alloc] init];
BOOL bCrlf = YES;
[sb AppendLine: @"[" crlf: bCrlf];
[sb AppendLine: @"  {" crlf: bCrlf];
[sb AppendLine: @"    \"telefones\": [" crlf: bCrlf];
[sb AppendLine: @"      {" crlf: bCrlf];
[sb AppendLine: @"        \"numero\": \"19995555555\"," crlf: bCrlf];
[sb AppendLine: @"        \"tipo\": \"T\"," crlf: bCrlf];
[sb AppendLine: @"        \"id\": 2541437" crlf: bCrlf];
[sb AppendLine: @"      }" crlf: bCrlf];
[sb AppendLine: @"    ]," crlf: bCrlf];
[sb AppendLine: @"    \"cnpj\": \"11395551000164\"," crlf: bCrlf];
[sb AppendLine: @"    \"rua\": \"R XAVIER AUGUSTO ROGGE, 22\"," crlf: bCrlf];
[sb AppendLine: @"    \"complemento\": \"\"," crlf: bCrlf];
[sb AppendLine: @"    \"contatos\": [" crlf: bCrlf];
[sb AppendLine: @"    ]," crlf: bCrlf];
[sb AppendLine: @"    \"tipo\": \"J\"," crlf: bCrlf];
[sb AppendLine: @"    \"razao_social\": \"SOUP BRASIL LTDA - ME\"," crlf: bCrlf];
[sb AppendLine: @"    \"nome_fantasia\": \"SOUP BRASIL\"," crlf: bCrlf];
[sb AppendLine: @"    \"bairro\": \"ABC DOS COLIBRIS\"," crlf: bCrlf];
[sb AppendLine: @"    \"cidade\": \"TEST\"," crlf: bCrlf];
[sb AppendLine: @"    \"inscricao_estadual\": \"222.102.222.116\"," crlf: bCrlf];
[sb AppendLine: @"    \"observacao\": \"\"," crlf: bCrlf];
[sb AppendLine: @"    \"id\": 2209595," crlf: bCrlf];
[sb AppendLine: @"    \"ultima_alteracao\": \"2016-12-26 16:22:34\"," crlf: bCrlf];
[sb AppendLine: @"    \"cep\": \"13555000\"," crlf: bCrlf];
[sb AppendLine: @"    \"suframa\": \"\"," crlf: bCrlf];
[sb AppendLine: @"    \"estado\": \"SP\"," crlf: bCrlf];
[sb AppendLine: @"    \"emails\": [" crlf: bCrlf];
[sb AppendLine: @"      {" crlf: bCrlf];
[sb AppendLine: @"        \"email\": \"somebody@terra.com.br\"," crlf: bCrlf];
[sb AppendLine: @"        \"tipo\": \"T\"," crlf: bCrlf];
[sb AppendLine: @"        \"id\": 1065557" crlf: bCrlf];
[sb AppendLine: @"      }" crlf: bCrlf];
[sb AppendLine: @"    ]," crlf: bCrlf];
[sb AppendLine: @"    \"excluido\": false" crlf: bCrlf];
[sb AppendLine: @"  }," crlf: bCrlf];
[sb AppendLine: @"  {" crlf: bCrlf];
[sb AppendLine: @"    \"telefones\": [" crlf: bCrlf];
[sb AppendLine: @"    ]," crlf: bCrlf];
[sb AppendLine: @"    \"cnpj\": \"12496555500180\"," crlf: bCrlf];
[sb AppendLine: @"    \"rua\": \"AV ROLF WIEST, 100\"," crlf: bCrlf];
[sb AppendLine: @"    \"complemento\": \"ANDAR 7 SALA 612 A 620\"," crlf: bCrlf];
[sb AppendLine: @"    \"contatos\": [" crlf: bCrlf];
[sb AppendLine: @"    ]," crlf: bCrlf];
[sb AppendLine: @"    \"tipo\": \"J\"," crlf: bCrlf];
[sb AppendLine: @"    \"razao_social\": \"SIMPLE SOFTWARE LTDA\"," crlf: bCrlf];
[sb AppendLine: @"    \"nome_fantasia\": \"\"," crlf: bCrlf];
[sb AppendLine: @"    \"bairro\": \"DOM ZETIRO\"," crlf: bCrlf];
[sb AppendLine: @"    \"cidade\": \"APARTVILLE\"," crlf: bCrlf];
[sb AppendLine: @"    \"inscricao_estadual\": \"\"," crlf: bCrlf];
[sb AppendLine: @"    \"observacao\": \"\"," crlf: bCrlf];
[sb AppendLine: @"    \"id\": 2255594," crlf: bCrlf];
[sb AppendLine: @"    \"ultima_alteracao\": \"2016-12-26 16:28:31\"," crlf: bCrlf];
[sb AppendLine: @"    \"cep\": \"89255505\"," crlf: bCrlf];
[sb AppendLine: @"    \"suframa\": \"\"," crlf: bCrlf];
[sb AppendLine: @"    \"estado\": \"SC\"," crlf: bCrlf];
[sb AppendLine: @"    \"emails\": [" crlf: bCrlf];
[sb AppendLine: @"    ]," crlf: bCrlf];
[sb AppendLine: @"    \"excluido\": false" crlf: bCrlf];
[sb AppendLine: @"  }," crlf: bCrlf];
[sb AppendLine: @"  {" crlf: bCrlf];
[sb AppendLine: @"    \"telefones\": [" crlf: bCrlf];
[sb AppendLine: @"      {" crlf: bCrlf];
[sb AppendLine: @"        \"numero\": \"1938655556\"," crlf: bCrlf];
[sb AppendLine: @"        \"tipo\": \"T\"," crlf: bCrlf];
[sb AppendLine: @"        \"id\": 2555438" crlf: bCrlf];
[sb AppendLine: @"      }" crlf: bCrlf];
[sb AppendLine: @"    ]," crlf: bCrlf];
[sb AppendLine: @"    \"cnpj\": \"00003555500153\"," crlf: bCrlf];
[sb AppendLine: @"    \"rua\": \"AV ABCDEF PINTO CATAO, 18\"," crlf: bCrlf];
[sb AppendLine: @"    \"complemento\": \"\"," crlf: bCrlf];
[sb AppendLine: @"    \"contatos\": [" crlf: bCrlf];
[sb AppendLine: @"      {" crlf: bCrlf];
[sb AppendLine: @"        \"telefones\": [" crlf: bCrlf];
[sb AppendLine: @"          {" crlf: bCrlf];
[sb AppendLine: @"            \"numero\": \"1999655554\"," crlf: bCrlf];
[sb AppendLine: @"            \"tipo\": \"T\"," crlf: bCrlf];
[sb AppendLine: @"            \"id\": 2555559" crlf: bCrlf];
[sb AppendLine: @"          }" crlf: bCrlf];
[sb AppendLine: @"        ]," crlf: bCrlf];
[sb AppendLine: @"        \"cargo\": \"zzz de compras\"," crlf: bCrlf];
[sb AppendLine: @"        \"nome\": \"Gerard\"," crlf: bCrlf];
[sb AppendLine: @"        \"emails\": [" crlf: bCrlf];
[sb AppendLine: @"          {" crlf: bCrlf];
[sb AppendLine: @"            \"email\": \"gerard@terra.com.br\"," crlf: bCrlf];
[sb AppendLine: @"            \"tipo\": \"T\"," crlf: bCrlf];
[sb AppendLine: @"            \"id\": 1065559" crlf: bCrlf];
[sb AppendLine: @"          }" crlf: bCrlf];
[sb AppendLine: @"        ]," crlf: bCrlf];
[sb AppendLine: @"        \"id\": 844485," crlf: bCrlf];
[sb AppendLine: @"        \"excluido\": false" crlf: bCrlf];
[sb AppendLine: @"      }" crlf: bCrlf];
[sb AppendLine: @"    ]," crlf: bCrlf];
[sb AppendLine: @"    \"tipo\": \"J\"," crlf: bCrlf];
[sb AppendLine: @"    \"razao_social\": \"TIDY TECNOLOGIA LTDA - EPP\"," crlf: bCrlf];
[sb AppendLine: @"    \"nome_fantasia\": \"TIDY\"," crlf: bCrlf];
[sb AppendLine: @"    \"bairro\": \"TUNA\"," crlf: bCrlf];
[sb AppendLine: @"    \"cidade\": \"JAGUAR\"," crlf: bCrlf];
[sb AppendLine: @"    \"inscricao_estadual\": \"395.222.441.222\"," crlf: bCrlf];
[sb AppendLine: @"    \"observacao\": \"ligar sempre depois das 14hs\"," crlf: bCrlf];
[sb AppendLine: @"    \"id\": 2255597," crlf: bCrlf];
[sb AppendLine: @"    \"ultima_alteracao\": \"2016-12-28 07:31:52\"," crlf: bCrlf];
[sb AppendLine: @"    \"cep\": \"13555500\"," crlf: bCrlf];
[sb AppendLine: @"    \"suframa\": \"\"," crlf: bCrlf];
[sb AppendLine: @"    \"estado\": \"SP\"," crlf: bCrlf];
[sb AppendLine: @"    \"emails\": [" crlf: bCrlf];
[sb AppendLine: @"      {" crlf: bCrlf];
[sb AppendLine: @"        \"email\": \"xi@tidy.com.br\"," crlf: bCrlf];
[sb AppendLine: @"        \"tipo\": \"T\"," crlf: bCrlf];
[sb AppendLine: @"        \"id\": 10655558" crlf: bCrlf];
[sb AppendLine: @"      }" crlf: bCrlf];
[sb AppendLine: @"    ]," crlf: bCrlf];
[sb AppendLine: @"    \"excluido\": false" crlf: bCrlf];
[sb AppendLine: @"  }" crlf: bCrlf];
[sb AppendLine: @"]" crlf: bCrlf];

// Load the JSON array into a JsonArray:
CkoJsonArray *jsonArray = [[CkoJsonArray alloc] init];
BOOL success = [jsonArray LoadSb: sb];
if (success != YES) {
    NSLog(@"%@",jsonArray.LastErrorText);
    return;
}

// Get some information from each record in the array.
int numRecords = [jsonArray.Size intValue];
int i = 0;
while (i < numRecords) {
    NSLog(@"%@%d%@",@"------ Record ",i,@" -------");

    CkoJsonObject *jsonRecord = [jsonArray ObjectAt: [NSNumber numberWithInt: i]];

    // Examine information for this record
    int numTelefones = [[jsonRecord SizeOfArray: @"telefones"] intValue];
    NSLog(@"%@%d",@"Number of telefones: ",numTelefones);
    int j = 0;
    while (j < numTelefones) {
        jsonRecord.J = [NSNumber numberWithInt: j];
        NSLog(@"%@%@",@"  telefones numero: ",[jsonRecord StringOf: @"telefones[j].numero"]);
        NSLog(@"%@%@",@"  telefones tipo: ",[jsonRecord StringOf: @"telefones[j].tipo"]);
        NSLog(@"%@%@",@"  telefones id: ",[jsonRecord StringOf: @"telefones[j].id"]);
        j = j + 1;
    }

    NSLog(@"%@%@",@"cnpj: ",[jsonRecord StringOf: @"cnpj"]);
    NSLog(@"%@%@",@"rua: ",[jsonRecord StringOf: @"rua"]);
    // ...

    int numContatos = [[jsonRecord SizeOfArray: @"contatos"] intValue];
    NSLog(@"%@%d",@"Number of contatos: ",numContatos);
    j = 0;
    while (j < numContatos) {
        jsonRecord.J = [NSNumber numberWithInt: j];

        numTelefones = [[jsonRecord SizeOfArray: @"contatos[j].telefones"] intValue];
        NSLog(@"%@%d",@"  Number of telefones: ",numTelefones);
        int k = 0;
        while (k < numTelefones) {
            jsonRecord.K = [NSNumber numberWithInt: k];
            NSLog(@"%@%@",@"  telefones numero: ",[jsonRecord StringOf: @"contatos[j].telefones[k].numero"]);
            NSLog(@"%@%@",@"  telefones tipo: ",[jsonRecord StringOf: @"contatos[j].telefones[k].tipo"]);
            NSLog(@"%@%@",@"  telefones id: ",[jsonRecord StringOf: @"contatos[j].telefones[k].id"]);
            k = k + 1;
        }

        NSLog(@"%@%@",@"  cargo: ",[jsonRecord StringOf: @"contatos[j].cargo"]);

        int numEmails = [[jsonRecord SizeOfArray: @"contatos[j].emails"] intValue];
        NSLog(@"%@%d",@"  Number of emails: ",numEmails);
        k = 0;
        while (k < numEmails) {
            jsonRecord.K = [NSNumber numberWithInt: k];
            NSLog(@"%@%@",@"  emails email: ",[jsonRecord StringOf: @"contatos[j].emails[k].email"]);
            NSLog(@"%@%@",@"  emails tipo: ",[jsonRecord StringOf: @"contatos[j].emails[k].tipo"]);
            NSLog(@"%@%@",@"  emails id: ",[jsonRecord StringOf: @"contatos[j].emails[k].id"]);
            k = k + 1;
        }

        j = j + 1;
    }

    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
// 
 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.