Sample code for 30+ languages & platforms
Rust

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 Rust Downloads

Rust

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

// Load the JSON array into a JsonArray:
let json_array = chilkat::JsonArray::new();
if json_array.load_sb(&sb).is_err() {
    println!("{}", json_array.last_error_text());
    return;
}

// Get some information from each record in the array.
let num_records = json_array.size();
let mut i = 0;
while i < num_records {
    println!("------ Record {} -------", i);

    let json_record = json_array.object_at(i).unwrap();

    // Examine information for this record
    let mut num_telefones = json_record.size_of_array("telefones");
    println!("Number of telefones: {}", num_telefones);
    let mut j = 0;
    while j < num_telefones {
        json_record.set_j(j);
        println!("  telefones numero: {}", json_record.string_of("telefones[j].numero").unwrap_or_default());
        println!("  telefones tipo: {}", json_record.string_of("telefones[j].tipo").unwrap_or_default());
        println!("  telefones id: {}", json_record.string_of("telefones[j].id").unwrap_or_default());
        j = j + 1;
    }

    println!("cnpj: {}", json_record.string_of("cnpj").unwrap_or_default());
    println!("rua: {}", json_record.string_of("rua").unwrap_or_default());
    // ...

    let num_contatos = json_record.size_of_array("contatos");
    println!("Number of contatos: {}", num_contatos);
    j = 0;
    while j < num_contatos {
        json_record.set_j(j);

        num_telefones = json_record.size_of_array("contatos[j].telefones");
        println!("  Number of telefones: {}", num_telefones);
        let mut k = 0;
        while k < num_telefones {
            json_record.set_k(k);
            println!("  telefones numero: {}", json_record.string_of("contatos[j].telefones[k].numero").unwrap_or_default());
            println!("  telefones tipo: {}", json_record.string_of("contatos[j].telefones[k].tipo").unwrap_or_default());
            println!("  telefones id: {}", json_record.string_of("contatos[j].telefones[k].id").unwrap_or_default());
            k = k + 1;
        }

        println!("  cargo: {}", json_record.string_of("contatos[j].cargo").unwrap_or_default());

        let num_emails = json_record.size_of_array("contatos[j].emails");
        println!("  Number of emails: {}", num_emails);
        k = 0;
        while k < num_emails {
            json_record.set_k(k);
            println!("  emails email: {}", json_record.string_of("contatos[j].emails[k].email").unwrap_or_default());
            println!("  emails tipo: {}", json_record.string_of("contatos[j].emails[k].tipo").unwrap_or_default());
            println!("  emails id: {}", json_record.string_of("contatos[j].emails[k].id").unwrap_or_default());
            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
//