Sample code for 30+ languages & platforms
Swift

Loading and Parsing a Complex JSON Array

See more JSON Examples

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

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

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

    // Load the JSON array into a JsonArray:
    let jsonArray = CkoJsonArray()!
    success = jsonArray.loadSb(sb: sb)
    if success != true {
        print("\(jsonArray.lastErrorText!)")
        return
    }

    // Get some information from each record in the array.
    var numRecords: Int = jsonArray.size.intValue
    var i: Int = 0
    while i < numRecords {
        print("------ Record \(i) -------")

        var jsonRecord: CkoJsonObject? = jsonArray.object(at: i)

        // Examine information for this record
        var numTelefones: Int = jsonRecord!.size(ofArray: "telefones").intValue
        print("Number of telefones: \(numTelefones)")
        var j: Int = 0
        while j < numTelefones {
            jsonRecord.j = j
            print("  telefones numero: \(jsonRecord!.string(of: "telefones[j].numero")!)")
            print("  telefones tipo: \(jsonRecord!.string(of: "telefones[j].tipo")!)")
            print("  telefones id: \(jsonRecord!.string(of: "telefones[j].id")!)")
            j = j + 1
        }

        print("cnpj: \(jsonRecord!.string(of: "cnpj")!)")
        print("rua: \(jsonRecord!.string(of: "rua")!)")
        // ...

        var numContatos: Int = jsonRecord!.size(ofArray: "contatos").intValue
        print("Number of contatos: \(numContatos)")
        j = 0
        while j < numContatos {
            jsonRecord.j = j

            numTelefones = jsonRecord!.size(ofArray: "contatos[j].telefones").intValue
            print("  Number of telefones: \(numTelefones)")
            var k: Int = 0
            while k < numTelefones {
                jsonRecord.k = k
                print("  telefones numero: \(jsonRecord!.string(of: "contatos[j].telefones[k].numero")!)")
                print("  telefones tipo: \(jsonRecord!.string(of: "contatos[j].telefones[k].tipo")!)")
                print("  telefones id: \(jsonRecord!.string(of: "contatos[j].telefones[k].id")!)")
                k = k + 1
            }

            print("  cargo: \(jsonRecord!.string(of: "contatos[j].cargo")!)")

            var numEmails: Int = jsonRecord!.size(ofArray: "contatos[j].emails").intValue
            print("  Number of emails: \(numEmails)")
            k = 0
            while k < numEmails {
                jsonRecord.k = k
                print("  emails email: \(jsonRecord!.string(of: "contatos[j].emails[k].email")!)")
                print("  emails tipo: \(jsonRecord!.string(of: "contatos[j].emails[k].tipo")!)")
                print("  emails id: \(jsonRecord!.string(of: "contatos[j].emails[k].id")!)")
                k = k + 1
            }

            j = j + 1
        }

        jsonRecord = nil
        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
    // 

}